React和Redux:如何在应用程序的任何位置获取应用程序的state / Redux存储的值?

时间:2017-01-06 11:29:31

标签: reactjs redux react-redux

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

1 个答案:

答案 0 :(得分:0)

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

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

每个reducer应被视为您要分组的州的一部分。 (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用于任何目的,包括发送请求

相关问题