Warning: useRouter must be used inside a <RouterProvider> component! Error Component Stack #2978
-
I am creating an authenticated route using the TanStack Router documentation. According to the docs, I need to create an inner component like this: function InnerApp() {
const auth = useAuth();
return (
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} context={{ auth }} />
</QueryClientProvider>
);
} Next, I need to pass this inner component inside the if (!rootElement.innerHTML) {
const root = ReactDOM.createRoot(rootElement);
root.render(
<AuthProvider>
<InnerApp />
</AuthProvider>
);
} Currently, I am using const router = useRouter();
const login = (token: string) => {
Cookies.set("token", token, { expires: 7 });
setIsAuthenticated(true);
router.invalidate();
}; However, How can I remove this warning, or how can I resolve this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
you currently have a circular dependency. to break it up, use the router not via the hook, but directly as returned from |
Beta Was this translation helpful? Give feedback.
just import the router returned from createRouter (if in another file) and directly reference it