const in React — React Technical Interview Questions & Answers
const in React
const in React
- const means the binding doesn’t get reassigned. Components are usually const Box = () => …. useState’s setter is const setN = … — you call setN(1), you don’t do setN = otherFn. const does not make the value on screen frozen; state still updates via the setter.
- let is fine for loop indexes outside JSX. Prefer const by default. Don’t confuse const with React.memo or Object.freeze.
- const in React — box and setN are const. Click still works — we call setN, we don’t do setN = something else.
Technical Interview Tip: const component + setter still updates UI.
Code Examples for const in React
Example
// const
import { useState } from "react";
const Box = () => {
const [n, setN] = useState(0);
return <button onClick={() => setN(n + 1)}>{n}</button>;
};
export default Box;
const in React — box and setN are const. Click still works — we call setN, we don’t do setN = something else.
How to Explain const in React to an Interviewer
Explain const as if you are teaching a junior — definition, then one tiny UI.
const : no rebinding. setState : call, don’t reassign.
Write 5–10 lines that use const and say what the UI does.
Show JSX Say the screen
What mistake do freshers make with const?
‘const means UI can’t change’.