clerk-react-patterns
This skill provides React SPA authentication patterns using Clerk's @clerk/react library for Vite and Create React App projects.
Identity proven by GitHub's verified organisation badge (clerk)
- Agents
- Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode
- License
- MIT (permissive)
- Pin
- a02dbd2pinned Oct 9, 2026
- Context cost
- about 940 tokenswhen the skill loads
- GitHub stars
- 86on clerk/skills
- Scripts
- 1 script
What it does
“React SPA auth patterns with @clerk/react for Vite/CRA - ClerkProvider setup, useAuth/useUser/useClerk hooks, React Router protected routes, custom sign-in flows. Triggers on: Vite Clerk setup, React Router auth, useAuth hook, protected route, custom sign-in form React.”
Generated summary from the skill's own text
This skill provides React SPA authentication patterns using Clerk's @clerk/react library for Vite and Create React App projects. It covers ClerkProvider setup, authentication hooks like useAuth and useUser, React Router protected routes, and custom sign-in flows. The skill includes practical examples and common pitfall solutions for client-side authentication.
When to use it
- Building Vite or CRA single-page apps requiring authentication
- Implementing React Router protected routes with Clerk
- Creating custom sign-in and sign-up forms in React
- Adding session tokens to API requests from React components
When not to
- Next.js projects should use clerk-nextjs-patterns instead
- TanStack Start projects should use clerk-tanstack-patterns instead
- Server-side authentication logic that requires auth() helper
Install
These commands install from the publisher's repository and need no account.
skills CLI
npx skills add clerk/skills --skill clerk-react-patternsInstalls from the repository's default branch, which may have changed since the commit we checked.
GitHub CLI 2.90 or later
gh skill install clerk/skills clerk-react-patterns@a02dbd2a933b8929525129a6cb9dcf6ed66d0adfInstalls the commit we checked (a02dbd2).
Copy by hand
Download the folder at a02dbd2 and put it in .claude/skills/clerk-react-patterns for Claude Code and in .agents/skills/clerk-react-patterns for Codex, Cursor, Gemini CLI, GitHub Copilot and OpenCode.
Copy for your agent
A prompt for any coding agent. It installs this skill from GitHub at the pinned commit, copies the files as they are, and runs nothing unless you ask.
Prompt
Install the agent skill "clerk-react-patterns" by Clerk. Source, pinned to commit a02dbd2a933b8929525129a6cb9dcf6ed66d0adf: https://github.com/clerk/skills/tree/a02dbd2a933b8929525129a6cb9dcf6ed66d0adf/skills/clerk-react-patterns 1. Download that folder at exactly this commit. Do not use the default branch. 2. Copy every file unchanged, keeping the folder structure, into .claude/skills/clerk-react-patterns/ for Claude Code and .agents/skills/clerk-react-patterns/ for Codex, Cursor, Gemini CLI, GitHub Copilot and OpenCode. Create the folders if they do not exist. 3. Do not edit the files, and do not run any script in the folder unless I ask you to. 4. When you are done, list the files you copied and where you put them. Listing and checks: https://www.agentnative.dev/skills/clerk/clerk-react-patterns
SKILL.md at the pin
---
name: clerk-react-patterns
description: 'React SPA auth patterns with @clerk/react for Vite/CRA - ClerkProvider
setup, useAuth/useUser/useClerk hooks, React Router protected routes, custom sign-in
flows. Triggers on: Vite Clerk setup, React Router auth, useAuth hook, protected
route, custom sign-in form React.'
license: MIT
allowed-tools: WebFetch
metadata:
author: clerk
version: 1.0.0
---
# React SPA Patterns
> This skill covers `@clerk/react` for Vite/CRA SPAs. For Next.js use `clerk-nextjs-patterns`. For TanStack Start use `clerk-tanstack-patterns`.
## What Do You Need?
| Task | Reference |
|------|-----------|
| useAuth / useUser / useClerk hooks | references/hooks.md |
| Protected routes with React Router | references/protected-routes.md |
| Custom sign-in / sign-up forms | references/custom-flows.md |
| React Router v6/v7 integration | references/router-integration.md |
## References
| Reference | Description |
|-----------|-------------|
| `references/hooks.md` | useAuth, isLoaded guard |
| `references/protected-routes.md` | ProtectedRoute pattern |
| `references/custom-flows.md` | useSignIn, useSignUp flows |
| `references/router-integration.md` | React Router v6/v7 setup |
## Setup
```
npm install @clerk/react
```
`.env`:
```
VITE_CLERK_PUBLISHABLE_KEY=pk_...
```
`src/main.tsx`:
```tsx
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { ClerkProvider } from '@clerk/react'
import App from './App.tsx'
const PUBLISHABLE_KEY = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY
createRoot(document.getElementById('root')!).render(
<StrictMode>
<ClerkProvider publishableKey={PUBLISHABLE_KEY}>
<App />
</ClerkProvider>
</StrictMode>,
)
```
## Mental Model
`@clerk/react` is client-only — there is no server-side `auth()`. All auth state comes from hooks.
- `isLoaded` must be `true` before trusting `isSignedIn` — always guard on `isLoaded`
- `useClerk()` gives access to `signOut`, `openSignIn`, `openUserProfile` and other methods
- `getToken()` from `useAuth()` fetches the session JWT for API calls
## Minimal Pattern
```tsx
import { useAuth } from '@clerk/react'
export function Dashboard() {
const { isLoaded, isSignedIn, userId } = useAuth()
if (!isLoaded) return <div>Loading...</div>
if (!isSignedIn) return <div>Please sign in</div>
return <div>Hello {userId}</div>
}
```
## Protected Route (React Router v6/v7)
```tsx
import { Navigate, Outlet } from 'react-router-dom'
import { useAuth } from '@clerk/react'
export function ProtectedRoute() {
const { isLoaded, isSignedIn } = useAuth()
if (!isLoaded) return <div>Loading...</div>
if (!isSignedIn) return <Navigate to="/sign-in" replace />
return <Outlet />
}
```
```tsx
<Routes>
<Route element={<ProtectedRoute />}>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/settings" element={<Settings />} />
</Route>
<Route path="/sign-in" element={<SignIn />} />
</Routes>
```
## Token for API Calls
```tsx
import { useAuth } from '@clerk/react'
export function DataFetcher() {
const { getToken } = useAuth()
async function fetchData() {
const token = await getToken()
if (!token) return
const res = await fetch('/api/data', {
headers: { Authorization: `Bearer ${token}` },
})
return res.json()
}
return <button onClick={fetchData}>Load</button>
}
```
## Common Pitfalls
| Symptom | Cause | Fix |
|---------|-------|-----|
| `isSignedIn` is `undefined` | `isLoaded` is still `false` | Always check `isLoaded` first |
| `ClerkProvider` missing | Provider not at root | Wrap `<App>` in `main.tsx` |
| Env var undefined | Wrong Vite prefix | Use `VITE_CLERK_PUBLISHABLE_KEY`, access via `import.meta.env` |
| Token is `null` | User not signed in | Null-check `getToken()` result |
| Sign-in component shows blank | No `publishableKey` on provider | Pass `publishableKey` explicitly |
## See Also
- `clerk-setup` - Initial Clerk install
- `clerk-custom-ui` - Custom flows & appearance
- `clerk-orgs` - B2B organizations
## Docs
[React SDK](https://clerk.com/docs/react/getting-started/quickstart)
From clerk/skills at a02dbd2, shown as plain text under its license: MIT (permissive). The license text is in skills/clerk-react-patterns/SKILL.md.
View SKILL.md on GitHub at a02dbd2Files
- SKILL.md4.2 KB · ab22780dd324
- evals/
- evals.json4.6 KB · d2468a89a047
- references/
- custom-flows.md2.7 KB · a675bc7eda25
- hooks.md2.4 KB · eb646879f054
- protected-routes.md2.2 KB · 2e612f390382
- router-integration.md2.1 KB · bd151fb30864
- templates/
- react-basic-auth/
- index.html300 B · c04a056bb17e
- package.json429 B · 70c62fb878f2
- src/
- App.tsx310 B · 8fb51aa6dc99
- main.tsx288 B · 759657eb7f01
- tsconfig.json234 B · aec98e2e964f
- vite.config.ts132 B · ccfbd497508a
12 files, 20 KB in total. Each file has a sha256 in the pin's manifest; the first 12 characters are shown.
Skill folder on GitHub at a02dbd2More from Clerk
audit-clerk-skill
Audits the bundled clerk-cli skill against the Clerk CLI source repository, comparing commands, flags, and agent-mode behavior to detect drift, gaps, and…
Claude Code · about 1.9K tokens · pinned 6 days ago
audit-expo-skill
Audits the clerk-expo skill against the @clerk/expo SDK source and clerk-docs to detect API drift, missing coverage, and stale claims.
Claude Code · about 2.2K tokens · pinned 6 days ago
clerk
This skill routes Clerk authentication tasks to specialized sub-skills based on the user's specific need, such as framework setup, CLI operations, custom UI…
Claude Code, Codex, Cursor +4 · about 1.4K tokens · pinned 6 days ago
clerk-android
This skill implements Clerk authentication in native Android apps written in Kotlin and Jetpack Compose by following the clerk-android SDK patterns and…
Claude Code, Codex, Cursor +4 · about 2K tokens · pinned 6 days ago
clerk-astro-patterns
This skill teaches the agent to integrate Clerk authentication into Astro applications using middleware, SSR pages, island components, and API routes.
Contains scripts
Claude Code, Codex, Cursor +3 · about 850 tokens · pinned 6 days ago
clerk-billing
Clerk Billing enables subscription management in SaaS apps by rendering pricing tables and checkout drawers, configuring plans with feature entitlements, and…
Claude Code, Codex, Cursor +4 · about 4.9K tokens · pinned 6 days ago
Related in Frontend and design
react-email
React Email lets agents build HTML email templates as React components and render them to HTML or plain text.
by ResendOfficial publisher
Claude Code, Codex, Cursor +4 · about 3.4K tokens · pinned 6 days ago
apollo-client
This skill guides building React applications with Apollo Client 4.x for managing GraphQL queries, mutations, and state.
by Apollo GraphQLOfficial publisher
Claude Code, Codex, Cursor +4 · about 1.7K tokens · pinned 6 days ago
stitch::code-to-design
This skill converts existing frontend code (React, Vue, Angular, etc.) into a Stitch Design by extracting HTML, analyzing design systems, and uploading both…
by GoogleOfficial publisher
Claude Code, Codex, Cursor +4 · about 760 tokens · pinned 6 days ago
stitch::extract-design-md
Extracts a comprehensive design system document (DESIGN.md) by analyzing frontend source code including component files, stylesheets, Tailwind configs, and…
by GoogleOfficial publisher
Claude Code, Codex, Cursor +4 · about 3.3K tokens · pinned 6 days ago
stitch::extract-static-html
This skill extracts a self-contained static HTML file from a running web application or React components by inlining all CSS and images as base64 data.
by GoogleOfficial publisher
Contains scripts
Claude Code, Codex, Cursor +3 · about 2.6K tokens · pinned 6 days ago
frontend-design
This skill builds production-grade frontend interfaces and web components with distinctive, intentional design aesthetics.
by SnykOfficial publisher
Claude Code, Codex, Cursor +4 · about 990 tokens · pinned 6 days ago