Installation
Setup
Creating the client
Providing the client
Wrap your app withConvexProvider:
Hooks
useQuery
Load reactive query data that automatically updates:"skip" to conditionally disable a query:
useMutation
Get a function to execute mutations:useAction
Get a function to execute actions: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:useConvex
Access the underlyingConvexReactClient:
useConvexConnectionState
Monitor the WebSocket connection state:Authentication components
Authenticated
Renders children only when authenticated:Unauthenticated
Renders children only when not authenticated:AuthLoading
Renders children while authentication is loading:Authentication
Set up authentication with an async token fetcher: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
undefinedloading state fromuseQuery - 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
useMutationanduseActionare stable across renders - Don’t pass React events directly to mutations - wrap them in handlers