Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Calling redirect() in a React Router component does nothing

ErrorThe 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.

  • Applies to React Router 7, React 19, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2You wrote an if with redirect() or navigate() directly in the component body.A redirect is needed while rendering, based on stateFix
    3. 3A 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

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 dev

Confirmed by raw.githubusercontent.com

Did this fix it?

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 onClick

2Fix

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 dev

Confirmed by raw.githubusercontent.com

Did this fix it?

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/dashboard

Confirmed by raw.githubusercontent.com

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.