javascript - React Router Route Params in Redux - Stack Overflow

Using redux and react router, I would like to access a route parameter on a ponent other than the one s

Using redux and react router, I would like to access a route parameter on a ponent 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="/" ponent={MainLayout} >
            <IndexRoute ponent={Home} />
            <Route path="reports">
                <Route path=":reportId" ponent={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 ponentWillReceiveProps lifecycle method inside of my ReportViewerContainer ponent. There must be a better way?

Using redux and react router, I would like to access a route parameter on a ponent 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="/" ponent={MainLayout} >
            <IndexRoute ponent={Home} />
            <Route path="reports">
                <Route path=":reportId" ponent={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 ponentWillReceiveProps lifecycle method inside of my ReportViewerContainer ponent. There must be a better way?

Share Improve this question asked Nov 2, 2016 at 4:20 cmwarrecmwarre 1171 silver badge7 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

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

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

You will find the better understanding with above example.

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 ponent as props.

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

import { withRouter } from 'react-router'

export default withRouter(ReportViewerContainer);

So I think you can use context to fetch the location that will be passed through the Router. ( Refer that link on how it works.) This should give you a solid direction to work on.

This can also help you down the lane to work with internationalization as well.

class MyComponent extends React.Component {
    static contextTypes = {
        router: React.PropTypes.object
    };

    render(){
        // By declaring context type here, and childContextTypes
        // on the parent along with a function with how to get it,
        // React will traverse up and look for the `router` context.
        // It will then inject it into `this.context`, making it 
        // available here.
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744216891a4563595.html

相关推荐

  • javascript - React Router Route Params in Redux - Stack Overflow

    Using redux and react router, I would like to access a route parameter on a ponent other than the one s

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信