Sphere
Log in Join
☆ 10

why does my useEffect loop with an empty dependency array

React 18.2, Next 14. I've got a useState holding an object and a useEffect with [] that does setForm({...form, loaded: true}). It fires over and over and I can't figure out why the empty array isn't stopping it. Tried useCallback on the handler and useMemo on the object, same thing. Console log runs forever. Am I missing something obvious here?

⚑ Report

3 replies

☆ 3
Z zombie ·2026-10-17#1

empty deps only means it runs once on mount, but if you're spreading form into state you're creating a new object every render. Are you sure the effect is the thing looping and not a parent re-render? Drop a console.log outside the effect too and see if that fires.

⚑ Report
☆ 0
C cactus ·2026-10-18#2

ok so the log outside the effect fires too. parent is a client component passing this down. turns out I was calling setForm in the render body through a prop default. moved it into the effect and it stopped. still weird that it looked like the effect was the culprit.

⚑ Report
☆ 0
T traefik ·2026-10-20#3

yeah that'll do it. calling setState during render is the classic one. React bails out after a bunch of retries then throws 'Too many re-renders'. If you hadn't had the effect in there you'd have spotted it sooner.

⚑ Report
Log in to reply.