Is it possible to call useRouteLoaderData within a component in RR7?
We have a microserivce thats responsible for returning the menu structure for a bunch of our micro front ends. We previously included the call to fetch the menu within our root loader, but realised that on every POST request an API call was being made to fetch the menu data which isn't ideal.
I thought I might be able to move the menu call out to a separate memoized menu component and move the Menu loader to its own routes/api/menu.ts file so that I could call useRouteLoaderData('menu') within the <Menu> component
The problem is that I keep getting undefined from useRouteLoaderData. In fact, any call to useRouteLoaderData that doesn't provide root as the argument returns undefined for me.
Here is my routes.ts file, which I can confirm all return the expected loader data + views where appropriate:
import { type RouteConfig, route, index } from '@react-router/dev/routes';
export default [
index('routes/_index.tsx'),
// GENERAL routes
route('health', 'routes/health.tsx'),
route('api/report/:id', 'routes/api.report.$id.tsx'),
route('details-server/:id', 'routes/details-server.$id.tsx'),
route('details/:id', 'routes/details.$id.tsx'),
route('logout', 'routes/logout.tsx'),
route('login', 'routes/login.tsx'),
// API routes
route('menu', 'routes/api/menu.tsx', { id: 'menu' }),
] satisfies RouteConfig;