question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux router state to have same shape as react-router props

See original GitHub 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, 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
tylercrossecommented, Aug 9, 2017

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 multiple Route components per ‘page’ is a cool idea, but in practice >95% of the time I just want a single match per route. YMMV 🙃

import { createSelector } from 'reselect'

const crewDataSelector = state => state.crews.data;
const pathnameSelector = state => state.router.location.pathname;

const crewIdSlector = createSelector(
  pathnameSelector,
  pathname => {
    const matches = pathname.match(/crew\/(\d+)/)
    if (matches) return matches[1];
  }
)

const currentCrewSelector = createSelector(
  crewIdSlector,
  crewDataSelector,
  (crewId, crewData) => crewData.crewId
)

export default currentCrewSelector
0reactions
purpleconescommented, Sep 18, 2017

I came across this same scenario. Has anyone been able to figure out a more elegant solution?

I ended wrapping my component with withRouter prior to connecting it to redux.

export default connect(mapStateToProps, mapDispatchToProps)(withRouter(MyComponent));

This adds a bunch of unused props to my component but works for now.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found