The React documentation claims that if you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects.
However that doesn't seem to be the case in that example:
function Child() {
useEffect(() => console.log("Re-render Child"));
return (<div>Child</div>);
}
export default function App() {
function Counter({ initialCount }) {
const [state, setState] = useState({value: initialCount });
useEffect(() => console.log("Re-render"));
return (
<>
Count: {state.value}
<br />
<br />
<button onClick={() => {
state.value += 1;
setState(state);
}}>+1</button>
<Child></Child>
</>
);
}
return (
<div>
<Counter initialCount={1} />
</div>
);
}
Clicking on the button only changes the inner property value, but the object remains the same, so why is React triggering a re-render (including a re-render of children and triggering the console.log effect)?
Here is a sandbox to test this: https://codesandbox.io/embed/react-hooks-counter-example-wrr2p
useEffect(() => console.log("Re-render"), []);Note that React may still need to render that specific component again before bailing out.Literally the next line in docs.[]argument.