Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v6] useParams() should return all params in the same <Routes> #7960

Closed
mjackson opened this issue Aug 18, 2021 · 2 comments
Closed

[v6] useParams() should return all params in the same <Routes> #7960

mjackson opened this issue Aug 18, 2021 · 2 comments

Comments

@mjackson
Copy link
Member

mjackson commented Aug 18, 2021

What is the new or updated feature that you are suggesting?

Currently (6.0.0-beta.1) useParams returns only the params that were part of that route's path. So e.g. in the following example, Root will never be able to access the userId param.

<Routes>
  <Route path="/" element={<Root />}>
    <Route path="/" element={<Home />} />
    <Route path="/users/:userId" element={<User />} />
  </Route>
</Routes>

function Root() {
  // No params here ... :'(
  let params = useParams(); // {}
}

function User() {
  let params = useParams(); // { userId: "..." }
}

However, this could be really useful for showing e.g. a login button or similar in the root route on a page.

Note: If User renders another nested <Routes>, it won't be possible for Root to get those params too because Root has already rendered before those <Routes> do. So useParams only knows about all params within a single <Routes>.

Implementation notes

Should be a fairly straightforward change. In useRoutes_ just collect all the params from all matches ahead of rendering instead of accumulating them in the reduceRight.

Why should this feature be included?

It will make it possible to know child params in parent routes. And that's awesome.

mjackson added a commit that referenced this issue Aug 20, 2021
* feat: Make all params available to nested routes (#7960)

* add test case

* update type imports

* bump file size limits

Co-authored-by: Michael Jackson <michael@jackson.us>
@mjackson
Copy link
Member Author

Fixed in #7963

@Rafael-Dabrowski
Copy link

Is there a particular reason for only using the same Routes object? As v6 is promoting nested Routes Objects shouldn't it be possible to have this behaviour for all nested Routes as well?
Eg:

<Routes>
  <Route path="/*" element={<Root />} />
</Routes>

function Root() {
  // No params here ... :'(
  let params = useParams(); // {}
  return (
    <Routes>
      <Route element={<Home />} />
      <Route path="/users/:userId" element={<User />} />
    </Routes>
  );
}

function User() {
  let params = useParams(); // { userId: "..." }
}

My Usecase for this is to be able to redirect to the first item in a collection if there is one available otherwise display a page where the user can add a new item to the collection. Or just to check if there is currently an item selected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants