Error: useNavigate() may be used only in the context of a <Router> component.
A React Router hook ran in a component that isn’t rendered inside a Router provider.
may be used only in the context of a <Router> componentuseNavigate() may be used onlyuseLocation() may be used only Meaning
Hooks like useNavigate, useLocation and useParams need router context. Components rendered outside <BrowserRouter>/<RouterProvider> (or in tests without a MemoryRouter) fail.
Common causes
- Router placed below the component using the hook
- App component using hooks outside its own BrowserRouter
- Tests rendering without a router
⚡ Quick fix
- Wrap the app with <BrowserRouter> at the root (index.jsx)
- In tests, render inside <MemoryRouter>
Detailed fix by platform
React
render(<MemoryRouter><Profile /></MemoryRouter>);
How to diagnose
- Tree — Is the component under the Router?
- Recent change — Did it start after a deploy, upgrade, or configuration change? Compare with the last working version.
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Adjacent JSX elements Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
- createContext only works in Client Components Next.js: You're importing a component that needs createContext. It only works in a Client Component but none of its parents are marked with "use client"
- Flask app context RuntimeError: Working outside of application context.
Most viewed in React
- React must be in scope ReferenceError: React is not defined / 'React' must be in scope when using JSX
- Functions cannot be passed Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".
- Objects are not valid as a React child Error: Objects are not valid as a React child
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026