Global authentication context
A React application's root entry point needs Clerk authentication available to all routes and components.
Clerk · Component
ClerkProvider supplies Clerk session and user context to React hooks and components. Put it at the app entry point for global authentication, or lower in the tree for selected routes.
ClerkProvider is the React context boundary for Clerk authentication. Place it around the whole application for global access, or lower in the tree when only selected routes or components need Clerk; configure it with the instance publishable key and optional navigation settings.
<ClerkProvider publishableKey={PUBLISHABLE_KEY} afterSignOutUrl="/"><App /></ClerkProvider>| Name | Type | Description |
|---|---|---|
publishableKey | string | The Clerk Publishable Key for your instance; when omitted, supported environment variables are read. |
afterSignOutUrl | string | The full URL or path to navigate to after signing out the current user is complete. |
A React provider component that supplies Clerk session and user context to descendants.
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { ClerkProvider } from '@clerk/react';
const publishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY;
if (!publishableKey) throw new Error('Add your Clerk Publishable Key');
ReactDOM.createRoot(document.getElementById('root')!).render(
<ClerkProvider publishableKey={publishableKey} afterSignOutUrl="/">
<App />
</ClerkProvider>,
);Wrap the application at its entry point so Clerk authentication context is available to the app.
A React application's root entry point needs Clerk authentication available to all routes and components.
Only a dashboard or protected route group uses Clerk features while public pages do not.
The application needs a defined destination after the active user signs out.
ClerkProvider supplies session and user context to Clerk hooks and components. It is normally placed at the application entry point so authentication is available throughout the React tree.
Follow-up: How would you limit Clerk to authenticated routes? Render ClerkProvider deeper in the component tree around only those routes or components.
Pass the instance's publishable key through the publishableKey property. In React, the provider can also read VITE_CLERK_PUBLISHABLE_KEY or CLERK_PUBLISHABLE_KEY when that property is omitted.
Follow-up: What should an application do if the key is missing? Fail during startup with a clear configuration error rather than rendering an unusable authentication flow.
The provider accepts configuration properties such as afterSignOutUrl, which controls where the user is sent after signing out. The documented React example wraps the app and sets this value to the root path.
Follow-up: Why might provider placement matter beyond authentication access? Moving it deeper lets unrelated parts of the application remain outside Clerk's context and configuration boundary.
useAuthuseUserSignedInSignedOutUserButton