-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Comments
* 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>
Fixed in #7963 |
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? <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. |
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'spath
. So e.g. in the following example,Root
will never be able to access theuserId
param.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 forRoot
to get those params too becauseRoot
has already rendered before those<Routes>
do. SouseParams
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 thereduceRight
.Why should this feature be included?
It will make it possible to know child params in parent routes. And that's awesome.
The text was updated successfully, but these errors were encountered: