我是新来的反应和减少。现在,我构建了第一个使用redux连接到api服务器的应用程序。在我的reducer文件中,我正在设置新状态。我的问题是如何在组件中访问该状态值?
您需要通过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中了解更多信息。