Constructor in Function Components — React Technical Interview Questions & Answers
Constructor in Function Components
Constructor in Function Components
- There is no constructor in a function component. People search this because class tutorials burned the word in. Initial state is the first argument to useState. Expensive init: useState(() => createRows()). That function runs once.
- Mount work (subscribe, fetch, focus) is useEffect, not a constructor body. Saying ‘functional constructor’ in a viva without this correction sounds confused.
- Constructor in Function Components — on screen: 0. First useState argument is the initial value — that is the ‘constructor’.
Parent
│ props (read-only)
▼
Child → UITechnical Interview Tip: useState initial ≈ class constructor state.
Code Examples for Constructor in Function Components
Example
// No constructor
import { useState } from "react";
export default function Box({ start = 0 }) {
const [n] = useState(start);
return <p>{n}</p>;
}
Constructor in Function Components — on screen: 0. First useState argument is the initial value — that is the ‘constructor’.
How to Explain Constructor in Function Components to an Interviewer
Explain Constructor in Function Components as if you are teaching a junior — definition, then one tiny UI.
No constructor. Init : useState(value) or lazy fn.
What does the example show on screen (or print), and what does that prove?
Constructor in Function Components — on screen: 0
What mistake do freshers make with Constructor in Function Components?
Mount : useEffect.