0

How can I do something similar to the below, without manually passing data into the function from a component?

function doSomething()
    return (dispatch, getState) => {
       console.log(getState().router.match.params)
    }
}

The reason this is important is because vital variables are stored in the URL in React (e.g. page for pagination, document ID, search input). When these are stored in Redux, it's super easy to access them and there's no need to pass them through from a component. But it seems like for match.params, this has to be manually passed through potentially several wrapped components into an action.

dan674
  • 1,848
  • 2
  • 15
  • 21
  • Don't break encapsulation. Put this information in Redux itself, then drive your router and thunks from Redux. – Gregory Higley Dec 13 '19 at 14:59
  • 1
    https://reacttraining.com/react-router/web/guides/redux-integration – Josh Pittman Dec 13 '19 at 15:07
  • Does [this](https://stackoverflow.com/questions/58588303/react-router-dom-redirect-from-redux-action-creator/58588534#58588534) answer help? – SuleymanSah Dec 13 '19 at 15:08
  • @SuleymanSah this doesn't give much more information than getState().router does - i.e. I get the pathname but I think the most important thing I'm looking for here is match (I'll update the question to make this clear). – dan674 Dec 16 '19 at 17:50
  • The reason match params are important is because important variables are stored in the URL in React, as opposed to in the redux store (e.g. page for pagination, or search input, or document ID). It's super easy to access variables from the redux store in actions, but if I want to access the variables kept in the URL this becomes very complicated and heavy, having to wrap and pass them through a component every time the action is called. This is why I couldn't get help from your suggestions @JoshPittman – dan674 Dec 16 '19 at 17:54
  • Also yours @GregoryHigley – dan674 Dec 16 '19 at 17:54
  • My suggestion ecnouraged you to try 'Connected React Router' https://github.com/supasate/connected-react-router if you need a deep integration. But it sounds like you just need to reference `window.location` and parse the data you need form the url directly. – Josh Pittman Dec 16 '19 at 18:13

0 Answers0