简体   繁体   中英

How to access route params from a component other then the Router component

In React-router 1.0/2.0 one can access routing params as this.props.params from a Route component - but there seems no supported way to access params from a component deeply contained in the above Route component.

The data sort of could be accessed through context.location but this one throws a warning, stating this is deprecated.

Starting in the latest version of React Router , all the context data has been deprecated in favor of this.context.router :

Only an object named router is added to context. Accessing this.context.history , this.context.location , and this.context.route are all deprecated. This new object contains the methods available from history (such as push , replace ) along with setRouteLeaveHook .

Unfortunately, this doesn't include the location data, so you'll need to pass it yourself into child (non-route handler) components.

Per the 2.0.0 upgrade guide, you should pass params or location down to child components yourself as needed. See https://github.com/rackt/react-router/blob/master/upgrade-guides/v2.0.0.md#accessing-location .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM