Router: [Question] How to get params in deeply nested component?

Created on 15 Aug 2018  路  4Comments  路  Source: reach/router

Hello, thanks a lot for your hard work. This router works really good. The only question which I didn't find in docs is how to pass params in deep nested component? In react-router we had withRouter HOC for this. Right know I need to pass in by hands. Any suggestions?

<comp1>
  <comp2>
    <comp3> // here I can get query params

Most helpful comment

I've just find in docs the official solution. There is a component Location which returns location on any level.

<Location>
  {({ location })=> {
    // ...
  }}
</Location>

Here is a link in docs

All 4 comments

I would reach for React's Context API

Would be nice to see somewhere in docs an official suggestion.

I think the official suggestion would be implied where it describes route components as "just React components", so there's nothing you can't do that you could do with any React Component, and I don't think there's going to be any proprietary solution for it.

So... you could do render props from a parent, React.cloneElement with parent params, Context etc..

I've just find in docs the official solution. There is a component Location which returns location on any level.

<Location>
  {({ location })=> {
    // ...
  }}
</Location>

Here is a link in docs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maieonbrix picture maieonbrix  路  4Comments

sseppola picture sseppola  路  4Comments

alexandernanberg picture alexandernanberg  路  3Comments

michaelwiktorek picture michaelwiktorek  路  4Comments

ricardobrandao picture ricardobrandao  路  5Comments