Skip to main content

Class: ConvexReactClient<API>

react.ConvexReactClient

A Convex client for use within React.

This loads reactive queries and executes mutations over a WebSocket.

Type parameters

NameTypeDescription
APIextends GenericAPIThe API of your application, composed of all Convex queries and mutations. npx convex codegen generates this type in convex/_generated/react.d.ts.

Constructors

constructor

new ConvexReactClient<API>(address, options?)

Type parameters

NameType
APIextends GenericAPI

Parameters

NameTypeDescription
addressstringThe url of your Convex deployment, often provided by an environment variable. E.g. https://small-mouse-123.convex.cloud.
options?ReactClientOptionsSee ReactClientOptions for a full description.

Methods

setAuth

setAuth(fetchToken): Promise<void>

Set the authentication token to be used for subsequent queries and mutations. fetchToken will be called automatically again if a token expires. fetchToken should return null if the token cannot be retrieved, for example when the user's rights were permanently revoked.

Parameters

NameTypeDescription
fetchTokenAuthTokenFetcheran async function returning the JWT-encoded OpenID Connect Identity Token

Returns

Promise<void>


clearAuth

clearAuth(): void

Clear the current authentication token if set.

Returns

void


watchQuery

watchQuery<Name>(name, args, journal?): Watch<ReturnType<NamedQuery<API, Name>>>

Construct a new Watch on a Convex query function.

Most application code should not call this method directly. Instead use the useQuery hook generated by npx convex codegen.

Type parameters

NameType
Nameextends string

Parameters

NameTypeDescription
nameNameThe name of the query function.
argsParameters<NamedQuery<API, Name>>The arguments to the query.
journal?QueryJournalAn (optional) QueryJournal to use while executing this query. Note that if this query function with these arguments has already been requested, the journal will have no effect.

Returns

Watch<ReturnType<NamedQuery<API, Name>>>

The Watch object.


mutation

mutation<Name>(name): ReactMutation<API, Name>

Construct a new ReactMutation.

Type parameters

NameType
Nameextends string

Parameters

NameTypeDescription
nameNameThe name of the Mutation.

Returns

ReactMutation<API, Name>

The ReactMutation object with that name.


action

action<Name>(name): ReactAction<API, Name>

Construct a new ReactAction

Type parameters

NameType
Nameextends string

Parameters

NameTypeDescription
nameNameThe name of the Action.

Returns

ReactAction<API, Name>

The ReactAction object with that name.


connectionState

connectionState(): ConnectionState

Get the current ConnectionState between the client and the Convex backend.

Returns

ConnectionState

The ConnectionState with the Convex backend.


close

close(): Promise<void>

Close any network handles associated with this client and stop all subscriptions.

Call this method when you're done with a ConvexReactClient to dispose of its sockets and resources.

Returns

Promise<void>

A Promise fulfilled when the connection has been completely closed.