Facebook Pixel

Why does my useEffect run infinitely?

Usually because you forgot the dependency array, or because you included a state variable that you update inside the effect. Each update triggers another render and another run. Fix the array so the effect only runs when intended.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in useEffect Dependency Array Mistakes That Cause Infinite Loops

The effect uses a stale version of that value and does not react when it changes. This causes bugs that are hard to trace because the code looks correct but behaves wrong. The hooks lint plugin catches these.

Because you included an object or function that is recreated on every render. The reference changes each time, so the effect runs every render. Memoize the value or move it inside the effect.

Use an empty dependency array. This tells React to run the effect only once after the initial mount and clean it up on unmount. Be sure you truly mean mount-only and are not skipping a real dependency.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.