What does useEffect do?
Model answer
useEffect runs code after React has rendered and updated the screen. It is for synchronising with things outside React: fetching data, subscribing to events, timers, or changing the document title.
You pass a function and a dependency array. The effect re-runs when a dependency changes. It can return a cleanup function that React runs before the next effect and when the component unmounts.
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 the dependency array of useEffect mean?
- 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?