gpt4 book ai didi

reactjs - 浅层 prop 比较对于连接的组件如何工作?

转载 作者:行者123 更新时间:2023-12-03 13:53:27 26 4
gpt4 key购买 nike

根据 redux 的文档,reducer 总是给出一个新的状态副本。在连接的组件中,react-redux 对 mapStateToProps(旧与新 props)中提到的属性进行浅层比较。

我的困惑是,由于 redux 总是提供新的状态副本,react-redux 进行的浅层比较应该总是给出 false,并且组件应该总是重新渲染。但这并没有发生。

有人可以解释一下我缺少什么吗?

我认为在mapStateToProp中读取的所有属性都是对象。

最佳答案

我们都同意,如果存储状态相同,mapStateToProps 将不会运行。 redux 文档如下:

The wrapper component generated by connect subscribes to the Redux store. Every time an action is dispatched, it calls store.getState() and checks to see if lastState === currentState. If the two state values are identical by reference, then it will not re-run your mapStateToProps function, because it assumes that the rest of the store state hasn't changed either.

答案为 Why isn't my component re-rendering, or my mapStateToProps running?可以描述您的情况,因为您可能不小心直接改变了您的状态。

由于您使用的是“combineReducers”(虽然这很棒),但还有一个“问题”:

combineReducers utility function tries to optimize state updates. If none of the slice reducers returned a new value, then combineReducers returns the old state object instead of a new one. This means that mutation in a reducer can lead to the root state object not being updated, and thus the UI won't re-render.

关于reactjs - 浅层 prop 比较对于连接的组件如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com