Skip to content

React Hooks

All exports are available from @requence/socketql/client/react.

Wraps URQL’s Provider and manages the WebSocket connection lifecycle. Calls connect() on mount. By default, connection errors are thrown to the nearest React error boundary. Pass onConnectError to handle them manually instead.

import { SocketQLProvider } from '@requence/socketql/client/react'
<SocketQLProvider client={client}>
<App />
</SocketQLProvider>
PropTypeDescription
clientSocketQLClientThe client returned by createClient
onConnect() => voidCalled on successful connection
onConnectError(error: ConnectionError) => voidCalled on connection error. When set, errors are not thrown to the error boundary.

When no onConnectError prop is provided, the provider throws the ConnectionError during render. Wrap it in an error boundary to handle rejections:

<ErrorBoundary fallback={<LoginPage />}>
<SocketQLProvider client={client}>
<App />
</SocketQLProvider>
</ErrorBoundary>

The same client can be reused after the error boundary resets — the provider will re-register its listeners and call connect() again.

Pass onConnectError to handle connection errors without an error boundary. This is useful when you want to call client.reconnect() after updating auth state:

<SocketQLProvider
client={client}
onConnectError={(err) => {
console.log(err.data) // e.g. { code: 'EXPIRED' }
refreshToken().then(() => client.reconnect())
}}
>
<App />
</SocketQLProvider>
import type { SocketQLClient } from '@requence/socketql/client/react'

Suspense-ready query hook. Throws on errors (use with an error boundary).

function useQuery<Data, Variables>(
args: UseQueryArgs<Variables, Data>
): readonly [Data, reexecuteFn]

Accepts all URQL UseQueryArgs options:

OptionTypeDescription
queryDocumentInputThe GraphQL query document
variablesVariablesQuery variables
requestPolicystringURQL request policy
pausebooleanPause the query

Returns a tuple of [data, reexecute]. data is the query result, and reexecute is a function to trigger a query execution. Throws the URQL error if the query fails.


Suspense-aware mutation hook with cache invalidation support.

function useMutation<Data, Variables>(
args: UseMutationArgs<Data, Variables>
): readonly [OperationResult, executeFn]
OptionTypeDefaultDescription
queryDocumentInputThe mutation document
invalidatestring | DocumentNode | ArrayQueries to invalidate on success
suspensebooleantrueWhether to trigger Suspense during execution
waitOnstring | DocumentNode | ArrayWait for these queries to re-resolve before un-suspending
waitOnTimeoutnumberTimeout in milliseconds to resolve the wait lock early

A tuple of [result, execute], same as URQL’s useMutation.


Thin wrapper around URQL’s useSubscription. Throws on errors (use with an error boundary).

function useSubscription<Data, Result, Variables>(
args: UseSubscriptionArgs<Variables, Data>,
handler?: SubscriptionHandler<Data, Result>
): readonly [Result | undefined, executeFn]
OptionTypeDescription
argsUseSubscriptionArgs<Variables, Data>URQL subscription arguments
handlerSubscriptionHandler<Data, Result>Optional handler to accumulate data across events

args accepts all URQL UseSubscriptionArgs options:

OptionTypeDescription
queryDocumentInputThe GraphQL subscription document
variablesVariablesSubscription variables
pausebooleanPause the subscription

A tuple of [data, execute]. data is the latest subscription result (or the accumulated result when using a handler). Throws the URQL error if the subscription encounters an error.