-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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]: Context prop in Outlet is not working with index
routes.
#8492
Comments
When |
@timdorr If |
You should be using the new useOutletContext(), not trying to access the route context directly. |
I use react-router-dom v6.0.2 does it support useOutletContext() |
No, that is new as of 6.1.0. Please upgrade to a newer version, as you'll also get some bug fixes included. |
Hi just to confirm, is it currently possible to access a parent's context from a grandchild route? I mean this kind of situation: <Routes>
<Route path="/" element={<Outlet context={{ someKey: 1 }} />}>
<Route path="child" element={<Outlet context={{ otherKey: 1 }} />}>
<Route index element={<GrandChildPage />} />
</Route>
</Route>
</Routes> then can we access the context containing |
@tmokmss I believe accessing the context from GrandChildPage is not working with |
Hi @umarjavedse thanks. Yeah I actually tried similar code as above but it did not work. <Routes>
<Route path="/" element={<Outlet context={{ someKey: 1 }} />}>
<Route path="child" element={<Outlet context={{ ...useOutletContext(), otherKey: 1 }} />}>
<Route index element={<GrandChildPage />} />
</Route>
</Route>
</Routes> |
This issue has been automatically marked stale because we haven't received a response from the original author in a while 🙈. This automation helps keep the issue tracker clean from issues that are not actionable. Please reach out if you have more information for us or you think this issue shouldn't be closed! 🙂 If you don't do so within 7 days, this issue will be automatically closed. |
Going to close this out as "working as expected" after chatting with @ryanflorence and @jacob-ebey.
const routes = [{
path: '/parent',
element: <Parent />,
children: [{
path: 'child',
element: <Child />
}],
}];
function Parent() {
// Since you can't do <Child prop={{ some: "value }} /> here, you can instead do:
return <Outlet context={{ some: "value" }} />
}
function Child() {
// Instead of receiving a `prop`, you can get the parent value from context
let obj = useOutletContext();
// obj => { some: "value" }
} If you need to provide something for deeper access through multiple intermediate Routes - then that's what normal |
What version of React Router are you using?
6.1.1
Steps to Reproduce
Setup project using below nested routes.
and then use
<Outlet context={data} />
in<Layout/>
Expected Behavior
Fetch
data
usinguseOutletContext
hook.Actual Behavior
Getting
undefined
withuseOutletContext
hook.The text was updated successfully, but these errors were encountered: