Calling redirect() in a React Router component does nothing
The URL stays the same after calling redirect('/login')Short answer
redirect() does not navigate by itself. It builds a Response with a Location header, and only a route loader or action hands that to the router. In a component use the useNavigate hook for events and effects, or render the Navigate component. Keep redirect() for loaders and actions, and throw it there.
Fix it fast
1Find out which cause you have
Find where redirect() is called. Inside a loader or action it is correct; anywhere else (component body, onClick, useEffect) it is the wrong tool.
grep -rn "redirect(" src/2Match what you see, then jump to its fix
- 1
The call runs (a log shows it) but the browser stays on the same page.redirect() is called inside a component or an event handlerFix - 2
You wrote an if with redirect() or navigate() directly in the component body.A redirect is needed while rendering, based on stateFix - 3
A loader checks the session, calls redirect('/login') as a plain statement, and the page still renders.In a loader, the redirect is created but not thrownFix
- 1
What does the error look like?
The URL stays the same after calling redirect('/login')redirect() returns a Response object and nothing else happens
Cause 1 of 3
redirect() is called inside a component or an event handler
1Check
redirect() returns a Response that sets the status code and the Location header. A loader or action returns that to the router. A click handler throws it away.
// Wrong: the Response is created and ignored
function LogoutButton() {
return <button onClick={() => redirect("/login")}>Log out</button>;
}2Fix
Get the navigate function from useNavigate() and call it. The docs also note that redirect belongs in action and loader functions rather than in the hook.
import { useNavigate } from "react-router";
function LogoutButton() {
const navigate = useNavigate();
return <button onClick={() => navigate("/login")}>Log out</button>;
}3Verify
Click the button; the address bar should change to /login.
npm run devConfirmed by raw.githubusercontent.com
Cause 2 of 3
A redirect is needed while rendering, based on state
1Check
Navigating while the component renders is a side effect in the wrong place. The Navigate component exists to do a redirect from render output.
grep -rn "navigate(" src/ | grep -v onClick2Fix
Return the Navigate component from render. Add replace so the back button does not return to the guarded page.
import { Navigate } from "react-router";
function Dashboard({ user }) {
if (!user) return <Navigate to="/login" replace />;
return <h1>Welcome</h1>;
}3Verify
Open the page while signed out; you should land on /login and Back should not loop.
npm run devConfirmed by raw.githubusercontent.com
Cause 3 of 3
In a loader, the redirect is created but not thrown
1Check
The docs show the redirect being thrown out of the loader. A bare call just builds a Response and drops it.
grep -rn -B2 "redirect(" app/routes/2Fix
Throw the result, as the docs example does, so the router receives it.
import { redirect } from "react-router";
export async function loader({ request }) {
if (!isLoggedIn(request)) {
throw redirect("/login");
}
return null;
}3Verify
Request the protected route signed out; the response should be 302 with a Location header.
curl -I http://localhost:5173/dashboardConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Use redirect() only in loaders and actions; use useNavigate for events and Navigate for render-time guards.
- Validate any user-supplied URL before passing it to redirect(), since it accepts absolute external URLs.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix