gpt4 book ai didi

reactjs - 为什么 redux 状态没有映射到 React 状态?

转载 作者:行者123 更新时间:2023-12-03 14:06:44 24 4
gpt4 key购买 nike

我对 React-Redux 非常陌生,遇到了一种名为 ma​​pstatetoprops 的方法。

但是为什么 redux 状态没有映射到 React 状态?为什么它被映射到 props 呢?

最佳答案

在 React 中,state 属于组件,而 props 则从其父组件传递给组件。 mapStateToProps() 是一个在 connect() 内部使用的函数,用于将数据从 Redux 存储传递到组件。

state 和 props 的区别:https://codeburst.io/react-state-vs-props-explained-51beebd73b21

如果您想在组件状态中使用 Redux 存储中的数据,组件首先需要将其作为 prop 接收。然后,您可以将其映射到 getDerivedStateFromProps 中的组件状态。 https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops

当您以这种方式从 props 派生状态时,更新组件的状态不会更改 Redux 状态。

关于reactjs - 为什么 redux 状态没有映射到 React 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54873603/

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