Redux router state to have same shape as react-router props
See original GitHub issueI’m working on an app that has a page route that looks like:
/:USERNAME/photos/:PHOTO_ID
Before, I was using react-router-dom without redux, so when I needed to access those params I would get them from this.props.match which has a rich mapping of the current route.
{
isExact: true,
params: {
USERNAME: "paul"
PHOTO_ID: "123"
},
path: "/:USERNAME/photos/:PHOTO_ID",
url: "/paul/photos/123",
}
The issue is if other parts that the app need these variables it’s not clear how to share them. Enter Redux. However, using the react-router-redux binding I see that the state that’s returned looks like:
{
hash: "",
key: "057zsw",
pathname: "/paul/photos/123",
search: "",
state: undefined
}
There should be more info in the state similar to the match object passed down to components by react-router-dom. I could make a custom function to parse the parameters but that would be duplicating the work that’s already being done by react-router-dom. Thoughts?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Redux router state to have same shape as react-router props · Issue ...
I'm working on an app that has a page route that looks like: /:USERNAME/photos/:PHOTO_ID Before, I was using react-router-dom without redux, ...
Read more >Deep Redux Integration - React Router v5
We want to make the integration of React Router and Redux as seamless as ... Routing data is already a prop of most...
Read more >React Router with Redux: Understanding navigation state
Use React Router to declaratively navigate within your React and Redux applications and maintain state across your app's navigation ...
Read more >Update Redux State based on Reach Router Component
It is a react lifecycle method it will be triggered when component will mount. You can update state from inside of this method....
Read more >Redux Essentials, Part 4: Using Redux Data
The official Redux Essentials tutorial: learn how to work with complex Redux state in React components.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

A workaround I’ve used is to create a selector (ala
reselect) and pull out the ‘params’ via a regex. This smells bad / feels hacky to me though. Having multipleRoutecomponents per ‘page’ is a cool idea, but in practice >95% of the time I just want a single match per route. YMMV 🙃I came across this same scenario. Has anyone been able to figure out a more elegant solution?
I ended wrapping my component with
withRouterprior toconnecting it to redux.This adds a bunch of unused props to my component but works for now.