Sub topic 4
Dependencies: controlling when an effect re-runs
This chapter assumes you know useState, how a state change triggers a re-render, and the basic useEffect(setup) call from the previous chapter. Here you learn how the second argument, the dependency array, decides when an effect runs again.
Three forms of the second argument
| Argument | When the effect runs |
|---|---|
| No array | After every render |
| `[]` | After the first render only (mount) |
| `[a, b]` | After the first render, and after any render where `a` or `b` changed |
The dependency array does not make the effect read different values. It only tells React when to skip the effect.
How React decides
After each render, React compares every value in the new array with the value from the previous render, one position at a time. The comparison is Object.is. Numbers, strings and booleans match when their values are equal. Objects, arrays and functions match only when they are the same reference. If every position matches, React skips the effect. If any position differs, React runs it.
List every reactive value
A reactive value is a prop, a piece of state, or anything you compute from them during rendering. If the effect reads a reactive value, that value belongs in the array.
import { useEffect } from 'react';
function Greeting({ name, greeting }) {
useEffect(() => {
document.title = `${greeting}, ${name}`;
}, [name, greeting]);
return <h1>{greeting}, {name}</h1>;
}The effect reads name and greeting, so both are listed. When either prop changes, the title updates. Values defined outside the component, such as imported constants, are not reactive and do not go in the array.
Common errors
- A missing dependency. If you write
[name]here, the title will not update when onlygreetingchanges. The effect keeps using thegreetingit saw when it last ran. - No array while setting state. An effect with no array runs after every render. If it also sets state, the state change causes a render, which runs the effect again, and the loop never ends.
import { useEffect, useState } from 'react';
function Broken() {
const [count, setCount] = useState(0);
useEffect(() => {
setCount(count + 1); // no array: runs after every render, so it loops forever
});
return <p>{count}</p>;
}- Using
[]to silence a problem. An empty array is correct only when the effect reads no reactive values. If it does read some,[]freezes them at their first-render values. - Removing a dependency to stop re-runs. If the effect re-runs too often, change the code. Do not delete the dependency. Later chapters cover the usual fixes: moving the value inside the effect, computing it during render, or using a functional state update.
Dependencies describe the effect
Decide the array from what the effect code uses, not from when you want it to run. If the array grows, the effect is telling you it synchronizes with more values. Use the react-hooks/exhaustive-deps lint rule to catch omissions. It reads the effect body and warns when the array and the code disagree.
Exercise
A component has props roomId and theme. Its effect connects to a chat server using only roomId, and it logs the theme value to the console on each connection. The array is [roomId]. What happens when only theme changes, and what should you do?
Show answer
The effect does not re-run, so no new log appears. Later runs would also log the theme captured when the effect last ran. Because the effect reads theme, the correct array is [roomId, theme]. But then a theme change would reconnect to the server. If you do not want that, remove the log from the effect, because it is not part of connecting to the room.