Skip to main content

Overview

Convex provides React hooks to seamlessly integrate your Convex backend with React applications. These hooks manage subscriptions, loading states, and reactivity automatically.

Query hooks

useQuery

Load reactive query data within a React component.
This hook subscribes to a Convex query and causes a rerender whenever the query result changes. The subscription is managed automatically - it starts when the component mounts and stops when it unmounts.
FunctionReference<'query'>
required
A function reference for the public query to run, like api.dir1.dir2.filename.func.
object | 'skip'
required
The arguments object for the query function, or the string "skip" to conditionally disable the query.
Returns: The query result, or undefined while loading. Throws: An error if the query encounters an error on the server.

Conditional queries

Pass "skip" as the second argument to conditionally disable a query:

useQueries

Load a variable number of reactive Convex queries.
This hook is similar to useQuery but allows loading multiple queries, which is useful for a dynamic number of queries without violating React hooks rules.
RequestForQueries
required
An object whose keys are identifiers and values are objects containing query (function reference) and args (arguments object).
Returns: An object with the same keys as the input. Values are the query result, undefined if still loading, or an Error if the query threw an exception.

usePaginatedQuery

Load data reactively from a paginated query to create a growing list.
This hook is designed for “infinite scroll” UIs. It concatenates all pages of results into a single list and manages continuation cursors automatically.
PaginatedQueryReference
required
A function reference to a public query that:
  • Has an argument named paginationOpts of type PaginationOptions
  • Returns a PaginationResult
object | 'skip'
required
The arguments object for the query function, excluding the paginationOpts property (which is injected by this hook), or "skip" to disable.
object
required
Returns: An object containing:
  • results - Array of currently loaded results
  • status - One of: "LoadingFirstPage", "CanLoadMore", "LoadingMore", or "Exhausted"
  • isLoading - Boolean indicating if currently loading
  • loadMore(numItems) - Function to fetch more results

Mutation hooks

useMutation

Construct a function to execute a Convex mutation.
The returned function is stable across renders (same reference identity), so it can be safely used in dependency arrays and memoization.
FunctionReference<'mutation'>
required
A function reference for the public mutation to run, like api.dir1.dir2.filename.func.
Returns: A ReactMutation function with:
  • (...args) - Execute the mutation, returns a promise of the result
  • withOptimisticUpdate(optimisticUpdate) - Configure an optimistic update

Optimistic updates

Optimistic updates provide instant UI feedback before the server responds:

Action hooks

useAction

Construct a function to execute a Convex action.
Actions can call third-party APIs and perform side effects. The returned function is stable across renders.
In most cases, calling an action directly from a client is an anti-pattern. Prefer having the client call a mutation that captures the user’s intent (by writing to the database) and then schedules the action via ctx.scheduler.runAfter. This ensures the intent is durably recorded even if the client disconnects.
FunctionReference<'action'>
required
A function reference for the public action to run, like api.dir1.dir2.filename.func.
Returns: A ReactAction function that executes the action and returns a promise of the result.

Provider and context hooks

ConvexProvider

Provides an active Convex client to descendants of this component.
Wrap your app in this component to use Convex hooks like useQuery, useMutation, and useAction.
ConvexReactClient
required
The ConvexReactClient instance to provide.
ReactNode
Child components that can use Convex hooks.

useConvex

Get the ConvexReactClient within a React component.
This relies on ConvexProvider being above in the React component tree. Returns: The active ConvexReactClient instance. Throws: An error if not used under ConvexProvider.

useConvexConnectionState

Get the current connection state and subscribe to changes.
This hook returns the current connection state and automatically rerenders when any part of the connection state changes. Returns: A ConnectionState object. Throws: An error if not used under ConvexProvider.

Authentication helpers

Authenticated

Conditionally render children only when the user is authenticated.

Unauthenticated

Conditionally render children only when the user is not authenticated.

AuthLoading

Conditionally render children while authentication status is being determined.

Custom auth integration

useConvexAuth

Get the auth state within a React component when using a custom auth integration.
This hook provides the current authentication state when using ConvexProviderWithAuth or a similar auth integration provider. It relies on an auth provider being above in the React component tree. Returns: A ConvexAuthState object containing:
boolean
required
Whether the authentication state is currently being loaded. true when initially determining auth state or during transitions between auth contexts.
boolean
required
Whether the user is currently authenticated with Convex. true only when both the auth provider reports authentication and Convex has successfully validated the token.
Throws: An error if not used under ConvexProviderWithAuth or a similar auth integration provider like ConvexProviderWithClerk.

ConvexAuthState

Type representing the state of an auth integration with Convex.
boolean
required
Whether the authentication state is currently being loaded.
boolean
required
Whether the user is currently authenticated with Convex.

ConvexProviderWithAuth

A replacement for ConvexProvider that integrates any auth provider with Convex.
This component wraps ConvexProvider and additionally provides ConvexAuthState to descendant components. Use this to integrate any auth provider with Convex by passing a custom useAuth hook. If the useAuth hook updates (causing a rerender), the auth state will transition to loading and fetchAccessToken() will be called again. This enables dynamic auth context changes like switching organizations. See Custom Auth Integration for more information.
ConvexReactClient
required
The ConvexReactClient instance to provide.
() => AuthHookResult
required
A React hook that returns the auth provider’s state and token fetcher.
ReactNode
Child components that can use Convex hooks and useConvexAuth().

Example with custom auth provider