2

Using https://github.com/reactjs/react-router-redux/

If I want to reload app (Redux) state based on url query params is the proper way to do that by getting the state.routing.locationBeforeTransitions.query property?

enter image description here

It works ... just wondering if that's the proper way.

2 Answers 2

5

Using a Redux connected component you can access the router's props from the 2nd argument of mapStateToProps:

function mapStateToProps (state, ownProps) {
  return {
    num: state.items.num,
    query: ownProps.location.query
  }
}

export default connect(mapStateToProps, null)(YourComponent)

See https://github.com/reactjs/react-router-redux#user-content-how-do-i-access-router-state-in-a-container-component .

You can also check out a demo here https://github.com/timarney/react-router-redux-query-params .

Sign up to request clarification or add additional context in comments.

Comments

1

From the docs:

You should not read the location state directly from the Redux store. This is because React Router operates asynchronously (to handle things such as dynamically-loaded components) and your component tree may not yet be updated in sync with your Redux state. You should rely on the props passed by React Router, as they are only updated after it has processed all asynchronous code.

The correct way is in the docs, but you can only do that in route components (those that you put in <Route path='/' component={Component} />).

I normally pass down the location property if I want to access it in some inner component.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.