Menu

React useEffect Dependencies — The Bug That Keeps Coming Back
📰

React useEffect Dependencies — The Bug That Keeps Coming Back

DEV Community: react·Mohsen Fallahnejad·12 days ago
#sK2Si2re
#devtoreact#devto#dev#useeffect#render
Reading 0:00
15s threshold

useEffect looks simple. Add a function, add a dependency array, done.

Then production hits you with:

a timer that logs the wrong count

a fetch that sends a stale token

an effect that runs forever

or React Strict Mode making everything run twice

These are not random React bugs. They all come from one idea:

Every render is a snapshot. Your effect remembers the values from the render that scheduled it.

Once you internalize that sentence, most useEffect pain disappears.

🧠 The Core Mental Model

When React renders your component, it creates a snapshot of props and state for that moment.

function Counter () { const [ count , setCount ] = useState ( 0 );

useEffect (() => { const id = setInterval (() => { console .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.