Search code examples
reactjsreact-contextreact-state-managementunstated

Does React update the parent contexts when a child context is updated?


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>

Solution

  • 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.