Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error after following RR7 quick start guide - useNavigate() may be used only in the context of a <Router> component #4826

Closed
4 tasks done
sanisoclem opened this issue Dec 23, 2024 · 6 comments

Comments

@sanisoclem
Copy link

Preliminary Checks

Reproduction

https://github.com/sanisoclem/clerk-rr7-repro

Publishable key

Description

Starting a new RR7 project and following the clerk guide doesn't produce a working app. The page errors out with:

useNavigate() may be used only in the context of a component.

Error: useNavigate() may be used only in the context of a component.
at invariant (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/react-router/dist/development/chunk-W3HZJLUQ.mjs:182:11)
at useNavigateUnstable (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/react-router/dist/development/chunk-W3HZJLUQ.mjs:4118:3)
at useNavigate (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/react-router/dist/development/chunk-W3HZJLUQ.mjs:4115:46)
at useAwaitableNavigate (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/@clerk[email protected][email protected][email protected]__react-router@[email protected]__ctw3iynjxr5uzeptdtabadn6ge/node_modules/@clerk/react-router/src/client/useAwaitableNavigate.tsx:8:20)
at ClerkProviderBase (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/@clerk[email protected][email protected][email protected]__react-router@[email protected]__ctw3iynjxr5uzeptdtabadn6ge/node_modules/@clerk/react-router/src/client/ReactRouterClerkProvider.tsx:36:29)
at Object.react-stack-bottom-frame (C:\Users\me\clerk-rr7-repro\node_modules.pnpm\[email protected][email protected]\node_modules\react-dom\cjs\react-dom-server.node.development.js:8723:18)
at renderWithHooks (C:\Users\me\clerk-rr7-repro\node_modules.pnpm\[email protected][email protected]\node_modules\react-dom\cjs\react-dom-server.node.development.js:46

A few things I have tried:

  • move ClerkProvider to layout - same outcome
  • downgrade to react 18 - same outcome

Steps to reproduce:

  1. npx create-react-router@latest my-react-router-app
  2. pnpm add @clerk/react-router
  3. continue following instructions in: https://clerk.com/docs/quickstarts/react-router
    • update root.tsx to use rootAuthLoader()
    • update root.tsx's default export App and wrap Outlet with ClerkProvider
  4. pnpm install
  5. pnpm dev

Expected behavior:

To be redirected to login page

Actual behavior:

Error is shown:

Oops!

useNavigate() may be used only in the context of a <Router> component.

Error: useNavigate() may be used only in the context of a <Router> component.
    at invariant (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/react-router/dist/development/chunk-W3HZJLUQ.mjs:182:11)
    at useNavigateUnstable (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/react-router/dist/development/chunk-W3HZJLUQ.mjs:4118:3)
    at useNavigate (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/react-router/dist/development/chunk-W3HZJLUQ.mjs:4115:46)
    at useAwaitableNavigate (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]__ctw3iynjxr5uzeptdtabadn6ge/node_modules/@clerk/react-router/src/client/useAwaitableNavigate.tsx:8:20)
    at ClerkProviderBase (file:///C:/Users/me/clerk-rr7-repro/node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]__ctw3iynjxr5uzeptdtabadn6ge/node_modules/@clerk/react-router/src/client/ReactRouterClerkProvider.tsx:36:29)
    at Object.react-stack-bottom-frame (C:\Users\me\clerk-rr7-repro\node_modules\.pnpm\[email protected][email protected]\node_modules\react-dom\cjs\react-dom-server.node.development.js:8723:18)
    at renderWithHooks (C:\Users\me\clerk-rr7-repro\node_modules\.pnpm\[email protected][email protected]\node_modules\react-dom\cjs\react-dom-server.node.development.js:4621:19)
    at renderElement (C:\Users\me\clerk-rr7-repro\node_modules\.pnpm\[email protected][email protected]\node_modules\react-dom\cjs\react-dom-server.node.development.js:5056:23)
    at retryNode (C:\Users\me\clerk-rr7-repro\node_modules\.pnpm\[email protected][email protected]\node_modules\react-dom\cjs\react-dom-server.node.development.js:5704:22)
    at renderNodeDestructive (C:\Users\me\clerk-rr7-repro\node_modules\.pnpm\[email protected][email protected]\node_modules\react-dom\cjs\react-dom-server.node.development.js:5530:11)

Environment

System:
    OS: Windows 11 10.0.26100
    CPU: (32) x64 AMD Ryzen 9 7950X3D 16-Core Processor
    Memory: 36.24 GB / 63.14 GB
  Binaries:
    Node: 22.12.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.15.0 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.26100.1882
  npmPackages:
    @clerk/react-router: ^0.1.5 => 0.1.5
    @react-router/dev: ^7.1.0 => 7.1.0
    @react-router/node: ^7.1.0 => 7.1.0
    @react-router/serve: ^7.1.0 => 7.1.0
    @types/node: ^20 => 20.17.10
    @types/react: ^19.0.1 => 19.0.2
    @types/react-dom: ^19.0.1 => 19.0.2
    autoprefixer: ^10.4.20 => 10.4.20
    cross-env: ^7.0.3 => 7.0.3
    isbot: ^5.1.17 => 5.1.18
    postcss: ^8.4.49 => 8.4.49
    react: ^19.0.0 => 19.0.0
    react-dom: ^19.0.0 => 19.0.0
    react-router: ^7.1.0 => 7.1.0
    tailwindcss: ^3.4.16 => 3.4.17
    typescript: ^5.7.2 => 5.7.2
    vite: ^5.4.11 => 5.4.11
    vite-tsconfig-paths: ^5.1.4 => 5.1.4
@sanisoclem sanisoclem added the needs-triage A ticket that needs to be triaged by a team member label Dec 23, 2024
@AdiRishi
Copy link

AdiRishi commented Dec 30, 2024

I was just migrating one of my repos to rr7 and @clerk/react-router and ran into this as well, would appreciate a solution :)

Happy to provide more information / reproductions if necessary.

@dburdan
Copy link

dburdan commented Dec 30, 2024

See here: #4641 (comment)

Temporarily downgrading Vite from v6 to v5 also worked for me.

@timothyclifford-loambio

We had the same issue, downgrading Node from 22.12 to 22.11 seemed to resolve.

@simonjodet
Copy link

We had the same issue, downgrading Node from 22.12 to 22.11 seemed to resolve.

I downgraded to node v20.18.1 using nvm and it fixed the issue.

@sanisoclem
Copy link
Author

See here: #4641 (comment)

Temporarily downgrading Vite from v6 to v5 also worked for me.

the repro is using vite: ^5.4.11

We had the same issue, downgrading Node from 22.12 to 22.11 seemed to resolve.

Thanks, this also worked for the app I am migrating.

Had an issue with the minimal repro though - the example given in step 4 of the guide says:

<SignInButton />: An unstyled component that links to the sign-in page. In this example, since no props or environment variables are set for the sign-in URL, this component links to the Account Portal sign-in page.

when clicking on the sign in button, it doesn't redirect to the account portal hosted signin page. Instead it gets redirected to: /signin which does not exist.

To make it work, had to create the /signin route:

import { SignIn } from '@clerk/react-router';

export default function SignInPage() {
  return (
    <div
      style={{
        backgroundImage: 'url(/bg.svg)',
        backgroundPosition: 'center',
        backgroundSize: 'cover'
      }}
      className="flex h-screen w-screen items-center justify-center"
    >
      <SignIn routing="path" path="/signin" />
    </div>
  );
}

and update routes.ts

import { type RouteConfig, index, route } from "@react-router/dev/routes";

export default [
  index("routes/home.tsx"),
  route("signin", "routes/signin.tsx"),
] satisfies RouteConfig;

@LekoArts LekoArts removed the needs-triage A ticket that needs to be triaged by a team member label Jan 6, 2025
@LekoArts
Copy link
Member

LekoArts commented Jan 6, 2025

Hi, this is an upstream issue with React Router and is tracked at remix-run/react-router#12475. Most often downgrading the Node.js version helped.

Please follow that upstream issue for a resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants