0

I am using unstated-next for state management. And for my application I am having multiple containers. Which created multiple React Contexts. And the providers are nested one under the other. You can assume like layers. Let layer 3 be the outermost layer and layer 1 being the innermost layer of the entire app. When I update the layer 1. Will the state within layer 2 and layer 3 get back to their initial state ?

         <Layer3.Provider initialState={[]}>
            <Layer2.Provider initialState={{}}>
              <Layer1.Provider>
my components

    </Layer1.Provider>
            </Layer2.Provider>
          </Layer3.Provider>
Shanmu
  • 3
  • 3

1 Answers1

0

Nope. A component's state is private to the component. Changing state of a child will never cause parent state to change. A parent state change might trigger a re-render for child but not the other way round.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • Thank you. I was thinking the same. I have BrowserRouter nested within the Layer 1. When the route changes the layer 1 updates. But it also changes the state of layer 2 and layer 3 to initialState. I am now wondering if it is the result of the BrowserRouter. Do you have any thoughts about this ? – Shanmu Dec 14 '22 at 10:24
  • I figured it out. The issue was with my routing. It was actually refreshing the page. Now its sorted and the state of parent's is never changed when state of child changes. – Shanmu Dec 15 '22 at 11:27