What does the dependency array of useEffect mean?
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
- What are hooks, and what are the rules of hooks?
- What does useEffect do?
- Why does an effect run twice in development?
- What is the cleanup function in useEffect, and when does it run?
- How do you fetch data in a React component with useEffect?
- What are race conditions in effects and how do you prevent them?