I'm building a react app that is using Tanstack router, and I'd like to access values from a context provider that I created over to the router beforeLoad handler for a given route. My app structure looks like this:
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<AppProvider>
<RouteProvider />
</AppProvider>
</React.StrictMode>
);
And the RouteProvider component definition is this:
import { RouterProvider as TSRouteProvider } from '@tanstack/react-router';
...
const RouteProvider: FC = () => {
const { user, signedIn, loaded } = useApp();
if (!loaded) {
return null;
}
return <TSRouteProvider router={router} context={{ user, signedIn }} />;
};
export default RouteProvider;
You can see above that I'm trying to use the context prop to pass values from my custom app context hook into the Tanstack router, however the tanstack router context never seems to receive the updated values. Is there a way to do this?
The
RouteProvideris not a real React Context, so it is basically static, however you caninvalidateit, thus triggering a refresh.A good way to handle this is with a single object behind a
useMemo:Now with
useEffectyou theninvalidatetherouterContextanytime it changes, now therouterContextshould work in yourbeforeLoadandloaderfunctions.And pass it in to
RouterProvider:It's insane that this is buried in the Tanstack Example here: https://tanstack.com/router/latest/docs/framework/react/examples/kitchen-sink-react-query-file-based, but not mentioned anywhere in the Tanstack docs for Router Context right now: https://tanstack.com/router/v1/docs/framework/react/guide/router-context.