繁体   English   中英

React 和 Redux:如何在我的应用程序中的任何一点获取应用程序状态/Redux 存储的值?

[英]React and Redux: How do I get the value of my application's state/Redux store at any point in my app?

我有一个 reactjs 网络应用程序。 我想向服务器发出请求并希望包含应用程序的整个状态。 我如何实现这一目标?

所以基本上你需要在使用状态 ( http://redux.js.org/docs/api/createStore.html ) 进行任何网络调用之前使用 createstore 库创建整个商店。 您可以按照自己的意愿使用组合减速器对树进行分组。 http://redux.js.org/docs/recipes/reducers/UsingCombineReducers.html )。 一个例子是

export default combineReducers({
  stateVariable1 : Reducer1,
  stateVariable2 : Reducer2...
});

每个减速器都应该被视为您要分组的状态的一部分。 ( http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html )

然后在应用程序的任何组件中,您都可以使用连接库将其与状态树连接起来,以便分派动作、获取状态项。 您还可以提及您想要访问状态树的哪一部分。

例如

const mapStateToProps = (state) => ({
  Reducer1 : StateVariable1 // Reducer1 in component can be accessed as this.props.Reducer1
});

const mapDispatchToProps = (dispatch) => ({
  quickinvestListingsActions : bindActionCreators(quickinvestListingsActions, dispatch) // Dispatching actions
});

export default connect(mapStateToProps, mapDispatchToProps)(QuickInvestListings); // This connects the component with the redux state

所以你可以将 Reducer1 用于任何目的,包括发送请求

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM