Add React Router to your Clerk-powered Chrome Extension
You will learn the following:
- Install
react-router-dom
- Create components for your routes
- Create layouts
- Wire layouts and routes up with
createMemoryRouter
Before you start
Example repository
This tutorial demonstrates how to integrate React Router into your Chrome Extension application using the Data API router.
Install react-router-dom
React Router is a lightweight, fully-featured routing library. To install it in your project, run the following command:
Create routes
- In the
src/
directory, create apopup/
directory. - In the
popup/
directory, create aroutes/
directory. - In the
routes/
directory, create thehome.tsx
,sign-in.tsx
,sign-up.tsx
, andsettings.tsx
files. - Use the following tabs to view the code necessary for each file.
Create layouts
- Delete your
src/popup.tsx
file. - In your
src/popup/
directory, create alayouts/
directory. - In the
layouts/
directory, create aroot-layout.tsx
file. - In the
root-layout.tsx
file, paste the following code to create a layout for your app.- The layout contains an
<Outlet />
component fromreact-router-dom
. This behaves similar to{children}
in Next.js or more generic React components. - The footer includes Clerk's
<UserButton />
component and a link to the/settings
page, which renders Clerk's<UserProfile />
component. Clerk's<SignedIn>
and<SignedOut>
control components determine what's displayed based on the user's authentication state.
- The layout contains an
Configure layouts and routes with createMemoryRouter
React Router's createMemoryRouter
is a router that uses memory to store the state of the router instead of the browser's history. This is useful for creating a router in a non-browser environment like a Chrome Extension.
- In the
src/popup/
directory, create anindex.tsx
file. - In the
index.tsx
file, paste the following code to configure your routes withcreateMemoryRouter
.
Test the integration
- Run your project with the following command:
- In your Chrome browser, open the extension popup. Ensure that the home page displays with a footer containing the Home, Sign In, and Sign Up links.
- Visit the Sign Up link and ensure the
<SignUp />
component is rendered. - Visit the Sign In link and ensure the
<SignIn />
component is rendered. Sign in with your account. You'll be redirected to the home page and the footer will display the Settings link and the<UserButton />
component. - Select the
<UserButton />
component to open the user menu. - Visit the Settings link and ensure the
<UserProfile />
component is rendered.
Feedback
Last updated on