-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Accessing nested params with useParams
#409
Comments
Hi @pReya, I don't think it's possible at the moment. In the current design, the context where parameters are stored gets overwritten in the Route, so parameters don't inherit. |
The way it currently works is pretty counterintuitive and makes using nested routes not an option in many cases. Intuitively something like this should work, and does work at least in react-router. <Route path="/:dataset" nest>
<Route path="/">
<ChartPicker />
</Route>
<Route path="/:type">
<Chart />
</Route>
</Route>; Merging existing params to the params context in |
I see. Your point is fair enough. I'm finalising everything for the v3 release (it's been in the rc for over 2 m already, so really want to publish it soon). Since it isn't a breaking change, perhaps we can get back to it in the next minor release? |
Minor release sounds reasonable, bit too late at this point with rc3 👍 |
it doesn't work like that, useParams returns just an empty object if you try to use it inside of a nested nest |
@molefrog please add this feature, otherwise I will have to go back to react-router-dom v5 :( |
We are using these light wrappers, seem to work fine (with the downside of having to import the custom implementations instead of wouter): import {useParams, Route} from "wouter";
type ParentParamsContextType = Record<string, string>;
const ParentParamsContext = createContext<ParentParamsContextType>({});
export function useNestedParams<T extends ParentParamsContextType>(): T {
const parentParams = useContext(ParentParamsContext);
const params = useParams<T>();
return {...parentParams, ...params} as T;
}
export const CustomRoute: typeof Route = props => {
const params = useNestedParams();
return (
<ParentParamsContext.Provider value={params}>
<Route {...props} />
</ParentParamsContext.Provider>
);
}; |
Kinda bizarre this is not already possible... |
@mattkrins I know... This feature isn't hard to implement, however this will require every Route in the app to subscribe to the context to get the outer parameters. It will mean that every route down the tree will re-render even if it was wrapped in |
Ok, actually I am wrong. Thoughts:
|
Hello, I have a component setup that looks something like this:
I'd like to access my
companyId
param within my<SeminarPage>
component, which is nested inside another route, viauseParams()
. However I always get an empty object. I assume becauseuseParams()
will always use the params from the closest ancestor<Route>
component. With nested routes, is there any way to access "higher up" params?The text was updated successfully, but these errors were encountered: