如何在React组件中获取应用程序(redux)状态?

问题描述 投票:0回答:1

我是新来的反应和减少。现在,我构建了第一个使用redux连接到api服务器的应用程序。在我的reducer文件中,我正在设置新状态。我的问题是如何在组件中访问该状态值?

reactjs redux react-redux
1个回答
2
投票

您需要通过Redux提供的connect组件将组件连接到Redux存储。 connect组件需要提供一个名为mapStateToProps的函数。此函数将告诉Redux您在组件中需要Redux状态的项目。

它看起来像这样:

function mapStateToProps(state) {
  return { yourStateKey: state.yourStateKey }
}

export default connect(mapStateToProps)(YourComponent)

您可以在appropriate section in the Redux docs中了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.