-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Which project does this relate to?
Router
Describe the bug
Firstly I'm not entirely sure if this is more a start or a router bug.
I'm using start but I think this lies on the router side.
Basically I have an _authenticted.tsx file route that I want to use to redirect to login if necessary, else render some routes and populate children.
I'm using beforeLoad to do a backend query to get my user object (or fail and redirect). And then populate the context with it.
eg.
export const Route = createFileRoute('/_authenticated')({
component: AuthenticatedComponent,
beforeLoad: async ctx => {
const user = await ctx.context.dataProvider.authService.getUser();
if (user === undefined) {
throw redirect({
to: '/login'
})
}
ctx.context.user = user;
}
})
But in the AuthenticatedComponent, if I have
const user = Route.useRouteContext(); this is undefined.
When I mouse over some elements it rerenders and seems to tell me the correct data.
I've tried using loader and useLoaderData but same much the same issue.
More than this, when I click a , the context object is back to its initial state from the createTanStackRouter call.
I don't necessarily need need to integrate with React's context as the docs mention here https://tanstack.com/router/v1/docs/framework/react/guide/authenticated-routes#authentication-using-react-contexthooks
Though I think I'm possibly confused at the where the line between Start's server function context, Router's context, and React context.
Would the design pattern for me to accomplish this be something like (similar to https://github.com/TanStack/router/blob/main/examples/react/authenticated-routes/src/auth.tsx):
- Create an authprovider which calls out to a server function on useEffect
- Provide this context in a react hook (useAuth)
- Use this hook in the beforeLoad function of the authenticated route
I don't understand really why or how this is different / better than what I was initially trying to do.
Bit confused about the order of operations if I were to use react hooks for this (esp with server functions), and and why docs suggest I could use useAuth which would be populated on initial render vs Route.useRouteContext which is not populated on initial render in the component.
Cheers
Your Example Website or App
https://stackblitz.com/edit/vitejs-vite-tzaxtk5x?terminal=dev
Steps to Reproduce the Bug or Issue
- Update some context value in beforeLoad.
- Console log the context in initial render and see not the updated value, but the initial value given to
createRouter - Click and to see the context value reset
Expected behavior
I expected to be able to use the updated context value, and I expected the context not to provide the initial value after following a
Also just not understanding why or how bringing react hooks into it improves this flow
Screenshots or Videos
No response
Platform
- Start Version: [^1.129.2]
- OS: macos
- Browser: *
- Browser Version: [e.g. 91.1]
- Bundler: vite
Additional context
No response