I have a component that doesn't appear to re-render even though the parent state has in fact updated.
Here is the (contrived) code:
const Index = () => {
render (
<Foo initialFoo={{ isTrue: true, count: 0 }} />
)
}
const Foo = ({ initialFoo }) => {
const [foo, setFoo] = useState({ ...initialFoo })
const reset = () => {
setFoo({ ...initialFoo })
}
return (
<>
{
foo.isTrue ?
<Bar initialBar={foo} reset={reset} /> :
<span>{foo.count}</span>
}
</>
)
}
const Bar = ({ initialBar, reset }) => {
const [bar, setBar] = useState({ ...initialBar })
return (
<div
onMouseOver={() => setBar({ ...bar, count: bar.count + 1 })}
onClick={reset}
>
<span>{bar.count}</span>
</div>
)
}
Hovering over the component increments count by 1 and displays it accordingly, but when I click on it, nothing changes when I expect the initial state to be displayed.
If I add logs in the Foo component, I can see that foo is updating back to the initial state after clicking on the component, but Bar isn't re-rendering even though it depends on foo. Shouldn't a change in state in the parent component trigger the children to re-render?
Note: I'm not looking for suggestions on how to write the code better/differently to achieve the desired result, rather just trying to understand the reason why the component is not displaying the original state after clicking on it in this current setup