beforeLoad not loading before the loader #5010
-
Don't think that's a bug yet, but rather a quirk that may be a bug. I'm using Then I expose the data loaded from loaderData to the page loader so I can use it on the UI by forwarding the context outputs to the loader output so I don't need to load it again. export const Route = createFileRoute({
component: MainLayout,
beforeLoad: async () => {
const loaderData = await rootLoader();
checkDomainAccess(loaderData.pca, loaderData.publicInfo.allowedDomains);
return loaderData;
},
loader: ({ context: { pca, publicInfo } }) => ({ pca, publicInfo }),
staleTime: Infinity,
}); For understanding, Then I use it on the layout component: const { useLoaderData, useNavigate } = getRouteApi("/_layout");
export default function MainLayout() {
const navigate = useNavigate();
const { pca, publicInfo } = useLoaderData();
...
} The problem is that sometimes the loader data (in this case Am I missing something here? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
can you please provide a complete minimal reproducer by forking one of the existing router stackblitz examples? |
Beta Was this translation helpful? Give feedback.
-
I found the error. My mistake. If I use beforeLoad to expose some data, I should use the right code would be: Routeexport const Route = createFileRoute({
component: MainLayout,
beforeLoad: async () => {
const loaderData = await rootLoader();
checkDomainAccess(loaderData.pca, loaderData.publicInfo.allowedDomains);
return loaderData;
},
}); Componentconst { useRouteContext, useNavigate } = getRouteApi("/_layout");
export default function MainLayout() {
const navigate = useNavigate();
const { pca, publicInfo } = useRouteContext();
...
} |
Beta Was this translation helpful? Give feedback.
I found the error. My mistake. If I use beforeLoad to expose some data, I should use
useRouteContext
instead of forwardinguseLoaderDate
.the right code would be:
Route
Component