简体   繁体   English

当子上下文更新时,React 会更新父上下文吗?

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

I am using unstated-next for state management.我正在使用 unstated-next 进行 state 管理。 And for my application I am having multiple containers.对于我的应用程序,我有多个容器。 Which created multiple React Contexts.它创建了多个 React 上下文。 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.假设第 3 层是最外层,第 1 层是整个应用程序的最内层。 When I update the layer 1. Will the state within layer 2 and layer 3 get back to their initial state?当我更新第 1 层时,第 2 层和第 3 层中的 state 是否会恢复到其初始的 state?

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

    </Layer1.Provider>
            </Layer2.Provider>
          </Layer3.Provider>

Nope.没有。 A component's state is private to the component.组件的 state 是组件私有的。 Changing state of a child will never cause parent state to change.改变孩子的 state 永远不会导致父母 state 改变。 A parent state change might trigger a re-render for child but not the other way round.父级 state 更改可能会触发子级的重新渲染,但反之则不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发送更新的父 state 时反应子道具不更新 - react child props does not update when sending updated parent state 在使用嵌套上下文的 React 中,如何防止在更新父上下文时重新渲染子上下文? - In React using nested context, how can you prevent child context from getting rerendered when the parent context is updated? 更新父状态时更新子组件 - Update child component when parent state is updated 当父 state 更改(钩子)时,React 子组件不会更新 - React child component does not update when parent state changes (hooks) 当子组件更新时反应更新父级 - React update parent when children component updated React Navigation:当父状态更新时,如何更新子状态? - React Navigation: How to update child state when parent state gets updated? React Navigation:当值来自redux并在child中更新时,如何更新父级的导航标题? - React Navigation: How to update navigation title for parent, when value comes from redux and gets updated in child? React Native子组件未从父组件接收更新状态 - React Native child component does not receive updated state from parent 在 React 中使用 Context 从 Parent 更新 Child 状态 - Update Child state from Parent using Context in React 在反应中添加到子项时更新父项中的数据 - Update data in parent when added in child in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM