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.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.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.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).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.PaginatedQueryReference
required
A function reference to a public query that:
- Has an argument named
paginationOptsof typePaginationOptions - 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
results- Array of currently loaded resultsstatus- One of:"LoadingFirstPage","CanLoadMore","LoadingMore", or"Exhausted"isLoading- Boolean indicating if currently loadingloadMore(numItems)- Function to fetch more results
Mutation hooks
useMutation
Construct a function to execute a Convex mutation.FunctionReference<'mutation'>
required
A function reference for the public mutation to run, like
api.dir1.dir2.filename.func.ReactMutation function with:
(...args)- Execute the mutation, returns a promise of the resultwithOptimisticUpdate(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.FunctionReference<'action'>
required
A function reference for the public action to run, like
api.dir1.dir2.filename.func.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.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.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.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.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.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 forConvexProvider that integrates any auth provider with Convex.
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().