79569447

Date: 2025-04-11 17:53:07
Score: 0.5
Natty:
Report link

was running into the same issue. The issue is that updating state in 1 beforeload (like __root) for another beforeload (like _auth) doesnt work. the state is correctly updated but the second beforeload still reads the stale data. that's just how react works unfortunately. using flush sync fixes it but it is flush sync. online people suggested using an external store outisde of react

my mock up code (hard coded just for testing routing)

export const Route = createRootRouteWithContext<MyRouterContext>()({
  beforeLoad: async ({ context }) => {
    if (!context.auth.isAuthenticated) {
      //  api auth/me call
      await new Promise((resolve) => setTimeout(resolve, 500));
      const apiToken = "1ausidhiausd2";
      const userData: User = {
        email: "[email protected]",
        provider: "EMAIL",
        username: "markmark",
      };
      const res = { accessToken: apiToken, user: userData };
      if (res) {
        flushSync(() => {
          context.auth.setUser(res.user);
        });
        context.auth.setAccessToken(res.accessToken);
        //context.auth.isAuthenticated = true;
      } else {
        context.auth.setUser(null);
        context.auth.setAccessToken(null);
        //context.auth.isAuthenticated = false;
      }
    }
  },
  component: RootComponent,
});
Reasons:
  • Blacklisted phrase (1): doesnt work
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: user29684365