Skip to main content
The Convex React library provides hooks and components for building reactive applications with real-time data synchronization.

Installation

Setup

Creating the client

Providing the client

Wrap your app with ConvexProvider:

Hooks

useQuery

Load reactive query data that automatically updates:
Basic usage:
Conditional queries: Pass "skip" to conditionally disable a query:

useMutation

Get a function to execute mutations:
Basic usage:
Optimistic updates:

useAction

Get a function to execute actions:
Basic usage:
Calling actions directly from clients is often an anti-pattern. Consider having the client call a mutation that records the user’s intent, then schedules the action via ctx.scheduler.runAfter.

usePaginatedQuery

Load paginated data for infinite scroll UIs:
Usage:
Return value:

useConvex

Access the underlying ConvexReactClient:
Usage:

useConvexConnectionState

Monitor the WebSocket connection state:
Usage:

Authentication components

Authenticated

Renders children only when authenticated:

Unauthenticated

Renders children only when not authenticated:

AuthLoading

Renders children while authentication is loading:
Combined example:

Authentication

Set up authentication with an async token fetcher:
Clear authentication:

Pagination helpers

Helper functions for optimistic updates in paginated queries:

insertAtTop

insertAtPosition

Best practices

  • Always use hooks inside React components or custom hooks
  • Handle the undefined loading state from useQuery
  • Use "skip" for conditional queries instead of conditional hooks
  • Wrap mutation/action calls in try-catch blocks
  • Use optimistic updates for better UX
  • The functions returned by useMutation and useAction are stable across renders
  • Don’t pass React events directly to mutations - wrap them in handlers

Type safety

All hooks are fully type-safe with generated API types: