Skip to main content

redirect

还没翻译译自英文(reactrouter.com)本页 15 段都还没译

redirect

Framework Data

Summary

Reference Documentation ↗

A redirect Response. Sets the status code and the Location header. Defaults to 302 Found.

This utility accepts absolute URLs and can navigate to external domains, so the application should validate any user-supplied inputs to redirects.

import { redirect } from "react-router";

export async function loader({ request }: Route.LoaderArgs) {
  if (!isLoggedIn(request))
    throw redirect("/login");
  }

  // ...
}

Params

url

The URL to redirect to.

init

The status code or a ResponseInit object to be included in the response.

Returns

A Response object with the redirect status and Location header.