What does the dependency array of useEffect mean?

React hooks and effects · Freshers

Model answer

It lists the reactive values (props, state, and variables derived from them) that the effect uses. With no array the effect runs after every render. With an empty array [] it runs once after the first render. With values it runs when any of them change.

Do not leave out dependencies to silence a warning. Fix the cause instead, for example by moving a function inside the effect.

Learn this topic

React useEffect Explained: Side Effects, Dependencies and Cleanup in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions