Sub topic 3
Your first useEffect: running code after render
This chapter assumes you know function components, JSX, useState, and how a state change triggers a re-render. It also assumes you know what a side effect is from the previous chapter. Here you write your first effect and learn when React runs it.
What useEffect does
useEffect is a hook that lets a component run code after React has rendered it and updated the DOM. Rendering must stay pure, so work that reaches outside the component belongs here. Examples are changing document.title, starting a timer, or sending a request.
The call takes two arguments:
useEffect(setup, dependencies);setupis a function. React calls it after the component renders.dependenciesis an optional array of values. It controls whensetupruns again. The next chapter covers it in detail.
A first example
This component keeps the browser tab title in sync with a counter:
import { useEffect, useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `Clicked ${count} times`;
}, [count]);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}Follow what happens:
- On the first render, React calls
Counter, which returns the button. - React adds the button to the DOM.
- React runs the effect, and the tab title becomes "Clicked 0 times".
- You click.
setCountschedules a re-render withcountequal to 1. - React renders again and updates the DOM. It then compares
[count]with the previous array. The value changed, so it runs the effect again.
If you click a button that does not change count, the effect is skipped.
When the effect runs
Normally the effect runs after the browser paints, so it does not delay what the user sees. Two consequences follow:
- The effect always sees the values from the render that created it. Inside the effect above,
countis a fixed number, not a live variable. - If the effect changes state, React renders again. Keep this in mind, because it is how infinite loops start.
Common errors
- Side effects in the render body. Writing
document.title = ...directly in the component works until rendering is interrupted or repeated. Put it in an effect. - Leaving out the dependency array. Without it, the effect runs after every render. That is rarely what you want.
- Setting state in an effect with no dependencies. The state change causes a render, the render runs the effect, and the effect sets state again. The loop never ends.
- Making the setup function
async. React expectssetupto return nothing or a cleanup function. Anasyncfunction returns a Promise. Define an async function inside the effect and call it instead.
Exercise
Exercise
In the Counter example, the dependency array is changed from [count] to [] (empty). The user clicks the button three times. What is the tab title afterward, and why?
Show answer
It stays "Clicked 0 times". An empty array tells React the effect depends on no values, so it runs only after the first render. Later renders do not run it again, so the title is never updated.