Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 675
Peter Aron Zentai Avatar asked Feb 09 '23 02:02

Peter Aron Zentai


1 Answers

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.

like image 145
Michelle Tilley Avatar answered Feb 10 '23 19:02

Michelle Tilley