React-router: Params is empty on root path

Created on 19 Jan 2018  路  6Comments  路  Source: ReactTraining/react-router

Version

react-router-dom: 4.1.1
react-router-config: ^1.0.0-beta.4

Test Case

https://codesandbox.io/s/j14yvr4rr9

Steps to reproduce

navigate to 'organisation info'

Expected Behavior

params.organisationGuid in Home component is 'some-organisation-guid'

Actual Behavior

params.organisationGuid in Home component is 'undefined'

Most helpful comment

@timdorr I really believe that this should be considered a bug. Not having the params available as a global object seems like a total anti-pattern, considering the URL is available globally. If I have a breadcrumbs component that exists across the entire application and it needs to be able to fetch data on occasion, I have to parse the URL manually when IMO it should inherently be possible while using the router.

All 6 comments

This is expected. params are not global, so they're not available to the ancestor components in the tree where the match occurs.

Is there a way to get params from children?

@timdorr I really believe that this should be considered a bug. Not having the params available as a global object seems like a total anti-pattern, considering the URL is available globally. If I have a breadcrumbs component that exists across the entire application and it needs to be able to fetch data on occasion, I have to parse the URL manually when IMO it should inherently be possible while using the router.

That is logically impossible. You can have <Route>s anywhere in your tree, with their own arbitrary params. There is not one canonical representation of the current <Route> and therefore the current match, only what is closest in the ancestor chain. We no longer have the concept of a central route config.

@goodbomb Exactly. Because of this decision I probably won't be available to upgrade my application from v3 to v4, since it's heavily dependent on params being accessible in various components.

Only way is to check various matchPath for every possible param everytime url changes and manually store it somewhere... But I fail to see how v4 is an upgrade.

@timdorr Please developers reconsider this. For multiple matches, "match" can be an array of objects, not?

It's not a matter of us deciding otherwise. It's simply not possible. Routes exist at any point in a tree, including underneath where you need to access those params.

You can use react-router-config to define a top-level route config and use that down the tree.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maier-stefan picture maier-stefan  路  3Comments

tomatau picture tomatau  路  3Comments

yormi picture yormi  路  3Comments

ackvf picture ackvf  路  3Comments

winkler1 picture winkler1  路  3Comments