Parent prop passed as child state not updating on parent prop update

146 Views Asked by At

I have recently come across situations where I want my child to re-render whenever the prop from the parent changes given the prop is passed as the initial value of state in Child.

But it is not working as expected.

Is this not the expected behavior?

As the state changes, I am expecting the child to re-render.

Currently, to achieve this I am using an useEffect that setStates every time prop from Parent changes.

Is there a better approach than this? and can mention some points that I am missing to understand this behavior.

To understand the context properly, I have made this codesandbox.

Expected: Child value should increment when Increment Parent is clicked without useEffect.

0

There are 0 best solutions below