T
TanStack2y ago
other-emerald

In latest release 1.28.4 useRouteContext()-hook returns undefined on first start

Hey 👋🏽 in latest release 1.28.4 the behavior of useRouteContext or beforeLoad has changed. I return the user from beforeLoad, but the useRouteContext-hook return undefined in the component.
export const Route = createFileRoute('/')({
beforeLoad: () => ({ user: 'Armin' }),
component: HomeComponent,
});

function HomeComponent() {
console.log('render HomeComponent');
const ctx = Route.useRouteContext(); // ctx is undefined
console.log(ctx);
return (
<div className="p-2">
<h3>Welcome Home! {ctx.user}</h3> // this fails!!! if I change it to ctx?.user the user-name appears, but only after joining the route again
</div>
);
}
export const Route = createFileRoute('/')({
beforeLoad: () => ({ user: 'Armin' }),
component: HomeComponent,
});

function HomeComponent() {
console.log('render HomeComponent');
const ctx = Route.useRouteContext(); // ctx is undefined
console.log(ctx);
return (
<div className="p-2">
<h3>Welcome Home! {ctx.user}</h3> // this fails!!! if I change it to ctx?.user the user-name appears, but only after joining the route again
</div>
);
}
Please checkout the reproduction. https://stackblitz.com/edit/tanstack-router-cfgel4?file=src%2Froutes%2Findex.tsx Update The issue was introduced in 1.28.2, maybe in https://github.com/TanStack/router/commit/d09db60415b4a6e8b32ec771f53015666a5c52e2 Thanks
3 Replies
other-emerald
other-emeraldOP2y ago
GitHub
Since version 1.28.2, the useRouteContext() hook returns undefined ...
Describe the bug It seems that in release 1.28.2 the behavior of useRouteContext or beforeLoad has changed. I return a context-change from beforeLoad, but the useRouteContext-hook return undefined ...
stormy-gold
stormy-gold2y ago
Fixed!
other-emerald
other-emeraldOP2y ago
Thanks @Tanner Linsley

Did you find this page helpful?