JavaScript React Router Route Params in Redux react router route params,react router route props,react router

Using redux and react router, I would like to access a route parameter on a component other than the one specified by the route.

I've got react-router, redux, and react-router-redux set up as follows and I would like to access the reportId parameter from my redux store.

const history = syncHistoryWithStore(browserHistory, store);

const store = createStore(reducers);

const routes = (
    <Router history={history}>
        <Route path="/" component={MainLayout} >
            <IndexRoute component={Home} />
            <Route path="reports">
                <Route path=":reportId" component={ReportViewerContainer}  />
            </Route>
        </Route>
    </Router>
);

ReactDOM.render(
    <Provider store={store}>{router}</Provider>,
    document.getElementById('root')
);

I've tried hooking up react-redux-router to store the route state, but I've found that there is nothing useful stored inside of redux besides the full path to the active route. This has left me with the options of either parsing out the reportId from the path in redux, or creating my own custom action in redux and updating it with the componentWillReceiveProps lifecycle method inside of my ReportViewerContainer component. There must be a better way?

Answer:1

If you want to access the router parameter inside the component, the best way to achieve this by using withRouter

https://egghead.io/lessons/javascript-redux-using-withrouter-to-inject-the-params-into-connected-components

You will find the better understanding with above example.

Answer:2

If you want to access the router in the redux store, there's a HOC called withRouter (docs). If I remember correctly, this will pass any router state to your component as props.

The only change you need to make to your component would be this:

import { withRouter } from 'react-router'

export default withRouter(ReportViewerContainer);
Answer:3

I am under the impression that ES6 classes are basically a syntactic sugar around the ES5 objects system. As I am trying to run React without a transpiler, I figured that I could use just use the old ...

I am under the impression that ES6 classes are basically a syntactic sugar around the ES5 objects system. As I am trying to run React without a transpiler, I figured that I could use just use the old ...

Is there a way to keep some sort of internal state while writing a curried function? For example, let's say I want to write a curried function that takes into account the number of times the function ...

Is there a way to keep some sort of internal state while writing a curried function? For example, let's say I want to write a curried function that takes into account the number of times the function ...

I am figuring out of i can make something like Google analytics script. They work with javascript to capture data and send it to an given URL. But i want it to do without ajax or REST API. What i ...

I am figuring out of i can make something like Google analytics script. They work with javascript to capture data and send it to an given URL. But i want it to do without ajax or REST API. What i ...

I am using this code to let user draw circles or images drawingManager = new google.maps.drawing.DrawingManager({ drawingMode: null, drawingControlOptions: { ...

I am using this code to let user draw circles or images drawingManager = new google.maps.drawing.DrawingManager({ drawingMode: null, drawingControlOptions: { ...

  1. wpf custom drawing control
  2. custom drawing treeview control
  3. custom control not drawing