The useEffect Hook
The useEffect hook runs code after the component renders. It is used for side effects, such as updating the document title or starting a timer.
The second argument is a dependency array. The effect runs again whenever a value in that array changes.
- Empty array means the effect runs once after the first render
- Listing values means it reruns when any of them change
- Return a function to clean up, such as clearing a timer
React.useEffect(() => {
document.title = "Hello";
}, []);
Always keep the dependency array in sync with the values the effect reads.
Try it yourself
Loading editor…