0
Challenge · 24Hooks & Effects

Effect Dependencies

Coreused in ~95% of apps

The second argument to useEffect causes more junior bugs than any other line in React: leave a value out and you ship a stale closure; put an object in and you spin an infinite loop. The fix is to stop treating it as a timing knob.

Core idea. The dependency array is not a setting you choose — it is an honest list of every reactive value (props, state, anything derived from them) your Effect reads. React re-runs the Effect whenever any listed value changes by Object.is. Your job is to make the list match what the code actually uses, then fix the cause when that list is a problem.
Playground, this isn't a screenshot
useEffect(() => {
// sync with an external system
}, [count])
count = 0text = "hi"
deps array holds
render log (newest first)
Press a button. Watch which renders run the effect and which skip it.
↑ play a little first, then dig in