ForgeFrontend — Prepare, Practice, Crack
Secure checkout
Lifetime access
Instant Drive delivery
Free updates forever

Prepare · Practice · Crack

What is lazy initialization of state and when do you need it?

Quick answer

Pass a function to useState so the expensive initial computation runs only once, on first render, instead of every render.

In detail

useState(expensive()) calls expensive() on every render and throws away the result after the first. useState(() => expensive()) passes an initializer function React calls only once. Use it when the initial value comes from a heavy computation, parsing, or reading localStorage.

// ❌ runs every render
const [v] = useState(parseHugeJSON(raw));
// ✅ runs once
const [v] = useState(() => parseHugeJSON(raw));

Why interviewers ask this: A subtle performance detail product companies like to probe.

This is 1 of 118+ questions in the React Interview Kit

Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.

⚡ Get the React Interview Kit → ₹399

Full kit

React Interview Kit · ₹399

Get it →