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
[Bug]: 6.0.0-beta.5 lost parent params in nested route #8073
Comments
Same bug appears to be causing issues for me on |
We've had a similar issue after updating. The route structure is as follows; <Route path="/view">
<Route
path={`typeA/*`}
element={<Layout type="a" />}
/>
<Route
path={`typeB/*`}
element={<Layout type="b" />}
/>
</Route> and the layout component then has a few subroutes: <Routes>
<Route
path={`subrouteA/:paramY/*`}
element={<... />}
/>
<Route
path={`subrouteB/:paramY/*`}
element={<... />}
/>
<Route
path="*"
element={<Empty />}
/>
</Routes> What seems to be happening is that the upper |
We've encountered the same bug, in |
@otakustay This was an intentional breaking change in beta.5. Sorry I forgot to put it in the release notes for beta.5. I just added them. The rationale is that we want to align If you need the params from a parent set of You could also ofc create your own context object and merge all the params for your whole app together using whatever logic you'd like. In this case, it's your responsibility to make sure there are no param name collisions and that you always render your const AllParamsContext = React.createContext({});
function AllParamsProvider({ children }) {
return <AllParamsContext.Provider value={useAllParams()} children={children} />;
}
function useAllParams() {
return {
// capture the params from all parent <Routes>
...React.useContext(AllParamsContext),
// capture the params from this <Routes>
...useParams()
};
}
function TopRoutes() {
return (
<AllParamsProvider>
<Routes>
<Route path=":type/*" element={<TypeRoute />} />
</Routes>
</AllParamsProvider>
);
}
function TypeRoute() {
return (
<AllParamsProvider>
<Routes>
<Route path=":id" element={<Page />} />
</Routes>
</AllParamsProvider>
);
}
function Page() {
let params = useAllParams();
// ...
} @Avyiel It sounds like you may have a related problem, but it has to do with splat routes instead of params. Maybe check #8074? |
This change is a bad design. Being able to easily access parent route params make it simple to split route definitions. The stated goal in the release notes states this is to make components with Routes portable but if you need that, just don't use parent params. I have a feeling this solution is for a less used scenario as well. |
I strongly agree with this, and was surprised when I read @mjackson's reply that this is intentional. From my experience it's a very common use case to want to read parent route params, and to no longer have support for this in the router itself feels less than ideal. |
It's ok to disagree with our design decisions. But when I give you our rationale, and a complete workaround to preserve the old behavior if you really want it, I'd appreciate a little more thoughtful response than "this is bad design". In general we favor explicit over implicit code. Getting params from some distant ancestor Locking this thread to prevent more piling on. |
We decided that this feature won't mess up our future plans for the router, so I'm going to add it back. Thanks for your patience and for bringing this to our attention. |
This was added back in 16214b2 and will be released today in beta.6 |
What version of React Router are you using?
6.0.0-beta.5
Steps to Reproduce
Expected Behavior
It should render
{type, id}
, withtype
as its parent route params andid
of its ownActual Behavior
It renders
{id}
,type
param is lostThe text was updated successfully, but these errors were encountered: