React Router Quickstart
Before you start
Example repository
React Router can be used in different modes: declarative, data, or framework. This tutorial explains how to use React Router in framework mode. To use React Router in declarative mode instead, see the dedicated guide.
This tutorial assumes that you're using React Router v7.1.2 or later in framework mode.
Create a new React app using React Router
If you don't already have a React app using React Router, run the following commands to create a new one.
npmcreatereact-router@latestclerk-react-router
cdclerk-react-router
npminstallpnpmcreatereact-routerclerk-react-router
cdclerk-react-router
pnpminstallyarncreatereact-routerclerk-react-router
cdclerk-react-router
yarninstallbunxcreate-react-routerclerk-react-router
cdclerk-react-router
buninstallInstall @clerk/react-router
The Clerk React Router SDK gives you access to prebuilt components, hooks, and helpers to make user authentication easier.
Run the following command to install the SDK:
npminstall@clerk/react-routerpnpmadd@clerk/react-routeryarnadd@clerk/react-routerbunadd@clerk/react-routerAdd the following keys to your .env file. These keys can always be retrieved from the API keys page in the Clerk Dashboard.
- In the Clerk Dashboard, navigate to the API keys page.
- In the Quick Copy section, copy your Clerk and .
- Paste your keys into your
.envfile.
The final result should resemble the following:
VITE_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEYAdd clerkMiddleware() and rootAuthLoader() to your app
clerkMiddleware() grants you access to user authentication state throughout your app. It also allows you to protect specific routes from unauthenticated users. To add clerkMiddleware() to your app, follow these steps:
- Since React Router middleware requires opting in via a future flag, add the following to your
react-router.config.tsfile:
importtype { Config } from'@react-router/dev/config'
exportdefault {
// ...
future: {
v8_middleware:true,
},
} satisfiesConfig- Add the following code to your
root.tsxfile to configure theclerkMiddleware()and rootAuthLoader() functions.
import { clerkMiddleware, rootAuthLoader } from'@clerk/react-router/server'
exportconstmiddleware:Route.MiddlewareFunction[] = [clerkMiddleware()]
exportconstloader= (args:Route.LoaderArgs) =>rootAuthLoader(args)
- By default,
clerkMiddleware()will not protect any routes. All routes are public and you must opt-in to protection for routes. See the clerkMiddleware() reference to learn how to require authentication for specific routes.
Add <ClerkProvider> and Clerk components to your app
The <ClerkProvider> component provides session and user context to Clerk's hooks and components. It's recommended to wrap your entire app at the entry point with <ClerkProvider> to make authentication globally accessible. See the reference docs for other configuration options.
Copy and paste the following code into your root.tsx file. This:
- Adds the
<ClerkProvider>to your app, providing Clerk's authentication context to your app. PassloaderDatato the provider so it can access the authentication data loaded byrootAuthLoader(). - Creates a header with Clerk's prebuilt components to allow users to sign in and out, and display different content for signed-in and signed-out users.
import { ClerkProvider, SignInButton, SignUpButton, Show, UserButton } from'@clerk/react-router'
import { isRouteErrorResponse, Links, Meta, Outlet, Scripts, ScrollRestoration } from'react-router'
import { clerkMiddleware, rootAuthLoader } from'@clerk/react-router/server'
// Pull in the `loaderData` from the `rootAuthLoader()` function
exportdefaultfunctionApp({ loaderData }:Route.ComponentProps) {
return (
// Pass the `loaderData` to the `<ClerkProvider>` component
<ClerkProviderloaderData={loaderData}>
<headerclassName="flex items-center justify-center py-8 px-4">
<Showwhen="signed-out">
<SignInButton />
<SignUpButton />
</Show>
<Showwhen="signed-in">
<UserButton />
</Show>
</header>
<Outlet />
</ClerkProvider>
)
}
}This example uses the following components:
- <Show when="signed-in">: Children of this component can only be seen while signed in.
- <Show when="signed-out">: Children of this component can only be seen while signed out.
- <UserButton />: Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.
- <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.
- <SignUpButton />: An unstyled component that links to the sign-up page. In this example, since no props or environment variables are set for the sign-up URL, this component links to the Account Portal sign-up page.
Run your project
Run your project with the following command:
npmrundevpnpmrundevyarndevbunrundevCreate your first user
- Visit your app's homepage at http://localhost:5173.
- Select "Sign up" on the page and authenticate to create your first user.
Next steps
Learn more about Clerk components, how to build custom authentication flows, and how to use Clerk's client-side helpers using the following guides.
Prebuilt components
Learn how to quickly add authentication to your app using Clerk's suite of components.
Create a custom sign-in-or-up page
Learn how to create a custom sign-in-or-up page with Clerk components.
Protect content and read user data
Learn how to use Clerk's hooks and helpers to protect content and read user data in your React Router app.
Get started with Organizations
Learn how to create and manage Organizations in your React Router app.
Feedback
Last updated on
