Peter Aron Zentai
Peter Aron Zentai

Reputation: 11740

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.

Upvotes: 4

Views: 5238

Answers (2)

Michelle Tilley
Michelle Tilley

Reputation: 159105

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.

Upvotes: 8

taion
taion

Reputation: 2837

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.

Upvotes: 0

Related Questions