Search code examples
reactjsreduxreact-router-redux

With react-router-redux, how should a non-route presentational component get the location?


Is there a good way to access the location from non-route components, where MapStateToProp's ownProps is not populated with the location? In my case, I need the location a few layers down from the routed component; avoiding lots of hierarchical parameter-passing is part of why we use Redux, so I'm not inclined to just pass the location down from the routed component through all the intervening layers. I've ended up using the routing store directly in mapStateToProps (warned against in the docs), as the best of bad choices.

The scenario is one where a set of common "frame" components are laid down by parent route, and they need to change slightly depending on context.


Solution

  • In v3.0.0 of react-router you can use withRouter to achieve this. For now, you should update your package.json to point to 3.0.0-alpha.1.

    There's a video from @gaeron: https://egghead.io/lessons/javascript-redux-using-withrouter-to-inject-the-params-into-connected-components