Ad

How To Access Route Params From A Component Other Then The Router Component

- 1 answer

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.

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad