Consider the following Component
const Component = () =>{
const [state, setState] = useState(null)
const onClick = () => setState('foo')
console.log(state)
return <button onClick={onClick}> Change </button>
}
- Before pressing the button
console
just printsnull
- First time button is pressed
console
printsfoo
- Second time button is pressend
console
printfoo
- Third time and forward
console
doesn't print anything
I understand that console
doesn't print anything cause I'm calling setState
passing the same value as the current state and React is bailing out the state update. My question is about the following assertion
Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go “deeper” into the tree. If you’re doing expensive calculations while rendering, you can optimize them with useMemo.
Why is this extra render necessary? I mean, Isn't Object.is
returning false
since the second click?