简体   繁体   中英

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

I have a reactjs web app. I want to make a request to server and want to include the whole state of the app. How do I achieve this?

So basically you need to have the entire store created with createstore library before doing any web call with the state ( http://redux.js.org/docs/api/createStore.html ) . You can structure your tree in such a way that you can group it as per your wish using combine reducer. ( http://redux.js.org/docs/recipes/reducers/UsingCombineReducers.html ). An example would be

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

Each reducer should be considered as a part of a state which you want to group. ( http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html )

Then in any component of your app you can use connect library to connect it with the state tree for dispatching actions, fetching state items. Also you can mention which part of the state tree you want to get access.

For example

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

So you can use Reducer1 for any purpose including sending request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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