need access to response of endpoint before any route
The structure of my app is -
In my AuthContext, I need access to the response of my /sessions endpoint on the initial load. So, in my root , I have -
```
export const Route = createRootRouteWithContext<RouterContext>()({
component: RootComponent,
beforeLoad: async ({ context }) => {
try {
const queryClient = context.queryClient;
const data = await queryClient.ensureQueryData(["sessions"], getSessions);
if (!data) {
throw redirect({ to: "/login" });
}
} catch (error: any) {
console.log("error", error);
// if (error) {
// throw redirect({ to: "/login" });
// }
// if (error.response?.status !== 401) {
// throw error;
// } else {
// // throw redirect({ to: "/login" });
// }
}
console.log("afterLoad");
},
errorComponent: () => <div>Error in root.tsx</div>,
pendingComponent: () => (
<div className="flex justify-center items-center h-screen">
Loading sessions...
</div>
),
});
```
2 Replies
magic-amberOP•5mo ago
Then, in my AuthContext, I have -
The issue is that in AuthContext, the isLoading is true. Isn't the point of the beforeLoad in __root that isLoading isn't true?
What am I missing?
eastern-cyan•5mo ago
please provide a complete minimal example, e.g. as a git repo