React Developer Interview Question

How does the useEffect dependency array work, and what tends to go wrong with it?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

React compares each dependency with its value from the previous render using Object.is and re-runs the effect only if one changed. An empty array means run once after mount; omitting the array means run after every render. It goes wrong when you leave a dependency out and capture a stale value, or when you include an object or function recreated every render, which makes the effect loop forever.

Why interviewers ask this

Effects are where most React bugs live, so the interviewer is probing whether you understand closures and render snapshots rather than treating the array as a magic incantation. They want to hear that you do not silence the lint rule, that you can name the difference between a stale closure and an infinite loop, and ideally that you question whether the effect needs to exist at all.

How to structure your answer

  • Explain the comparison and when the effect actually re-runs.
  • Describe the two classic failures: stale closure and render loop.
  • Say how you fix each, with functional updates, refs or hoisting.
  • Finish by questioning whether the effect belongs there at all.

Example answer

Spoken example, first person

React keeps the array from the last render and compares each item with Object.is. If everything matches it skips the effect; if anything differs it runs the cleanup for the old one and then the new one. The catch is that the effect closes over the props and state from the render that created it, so if I leave something out of the array I get a stale value that quietly goes out of date. The other side of it is putting an object or an arrow function in there, because those are new references every render, so the effect runs on a loop. My usual fixes are functional state updates so I do not need the current value as a dependency, hoisting constants out of the component, and only wrapping a callback when it genuinely has to be a dependency. Honestly though, the first question I ask is whether the effect should exist. Most effects I have deleted were computing derived state, which is just a calculation during render, or fetching data that belonged in the router or a query library instead.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How do you handle an effect that needs the latest value but should not re-run when it changes?
  • When would you use useLayoutEffect instead?
  • What runs first, the cleanup of the old effect or the new effect?

Related react developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot