Overview
ConvexReactClient is a stateful client that loads reactive queries and executes mutations over a WebSocket connection to your Convex backend.
Constructor
Create a newConvexReactClient instance to connect to your Convex deployment.
Parameters
The URL of your Convex deployment, typically provided by an environment variable.
For example:
https://small-mouse-123.convex.cloudOptional configuration object for the client.
Methods
mutation
Execute a mutation function on the server.A function reference for the public mutation to run, like
api.dir1.dir2.filename.func.An arguments object for the mutation. If omitted, the arguments will be
{}.action
Execute an action function on the server.A function reference for the public action to run, like
api.dir1.dir2.filename.func.An arguments object for the action. If omitted, the arguments will be
{}.query
Fetch a query result once (not reactive).Most application code should subscribe to queries using the
useQuery hook instead.
This method fetches the query result once without establishing a subscription.A function reference for the public query to run.
An arguments object for the query. If omitted, the arguments will be
{}.watchQuery
Construct a watch on a Convex query function.A function reference for the public query to run.
An arguments object for the query. If omitted, the arguments will be
{}.Watch object with methods:
onUpdate(callback)- Subscribe to query changes, returns unsubscribe functionlocalQueryResult()- Get the current result (orundefinedif not available)journal()- Get the current query journal
setAuth
Set the authentication token for subsequent queries and mutations.An async function returning the JWT-encoded OpenID Connect Identity Token.
Should return
null if the token cannot be retrieved.A callback that will be called when the authentication status changes.
clearAuth
Clear the current authentication token if set.connectionState
Get the current connection state between the client and the Convex backend.ConnectionState object with properties:
hasInflightRequests- Whether there are pending requestsisWebSocketConnected- Whether the WebSocket is connectedtimeOfOldestInflightRequest- Date of oldest pending request or nullhasEverConnected- Whether the client has ever connectedconnectionCount- Number of times the client has connectedconnectionRetries- Number of failed connection attemptsinflightMutations- Number of mutations in flightinflightActions- Number of actions in flight
subscribeToConnectionState
Subscribe to connection state changes.Function called whenever the connection state changes.
close
Close any network handles associated with this client and stop all subscriptions.Properties
url
Return the address for this client.Not guaranteed to match the address used in the constructor as it may be canonicalized.