Module: react
Tools to integrate Convex into React applications.
This module contains:
- ConvexReactClient, a client for using Convex in React.
- ConvexProvider, a component that stores this client in React context.
- Authenticated, Unauthenticated and AuthLoading helper auth components.
- Hooks useQuery, useMutation, useAction and more for accessing this client from your React components.
Usage
Creating the client
import { ConvexReactClient } from "convex/react";
// typically loaded from an environment variable
const address = "https://small-mouse-123.convex.cloud"
const convex = new ConvexReactClient(address);
Storing the client in React Context
import { ConvexProvider } from "convex/react";
<ConvexProvider client={convex}>
<App />
</ConvexProvider>
Using the auth helpers
import { Authenticated, Unauthenticated, AuthLoading } from "convex/react";
<Authenticated>
Logged in
</Authenticated>
<Unauthenticated>
Logged out
</Unauthenticated>
<AuthLoading>
Still loading
</AuthLoading>
Using React hooks
import { useQuery, useMutation } from "convex/react";
import { api } from "../convex/_generated/api";
function App() {
const counter = useQuery(api.getCounter.default);
const increment = useMutation(api.incrementCounter.default);
// Your component here!
}
Classes
Interfaces
Type Aliases
AuthTokenFetcher
Ƭ AuthTokenFetcher: (args
: { forceRefreshToken
: boolean
}) => Promise
<string
| null
| undefined
>
Type declaration
▸ (args
): Promise
<string
| null
| undefined
>
An async function returning the JWT-encoded OpenID Connect Identity Token if available.
forceRefreshToken
is true
if the server rejected a previously
returned token, and the client should try to fetch a new one.
See setAuth.
Parameters
Name | Type |
---|---|
args | Object |
args.forceRefreshToken | boolean |
Returns
Promise
<string
| null
| undefined
>
Defined in
browser/sync/authentication_manager.ts:23
ConvexAuthState
Ƭ ConvexAuthState: Object
Type representing the state of an auth integration with Convex.
Type declaration
Name | Type |
---|---|
isLoading | boolean |
isAuthenticated | boolean |
Defined in
OptionalRestArgsOrSkip
Ƭ OptionalRestArgsOrSkip<FuncRef
>: FuncRef
["_args"
] extends EmptyObject
? [args?: EmptyObject | "skip"] : [args: FuncRef["_args"] | "skip"]
Type parameters
Name | Type |
---|---|
FuncRef | extends FunctionReference <any > |
Defined in
Preloaded
Ƭ Preloaded<Query
>: Object
The preloaded query payload, which should be passed to a client component and passed to usePreloadedQuery.
Type parameters
Name | Type |
---|---|
Query | extends FunctionReference <"query" > |
Type declaration
Name | Type |
---|---|
__type | Query |
_name | string |
_argsJSON | string |
_valueJSON | string |
Defined in
PaginatedQueryReference
Ƭ PaginatedQueryReference: FunctionReference
<"query"
, "public"
, { paginationOpts
: PaginationOptions
}, PaginationResult
<any
>>
A FunctionReference that is usable with usePaginatedQuery.
This function reference must:
- Refer to a public query
- Have an argument named "paginationOpts" of type PaginationOptions
- Have a return type of PaginationResult.
Defined in
react/use_paginated_query.ts:31
UsePaginatedQueryResult
Ƭ UsePaginatedQueryResult<Item
>: { results
: Item
[] ; loadMore
: (numItems
: number
) => void
} & { status
: "LoadingFirstPage"
; isLoading
: true
} | { status
: "CanLoadMore"
; isLoading
: false
} | { status
: "LoadingMore"
; isLoading
: true
} | { status
: "Exhausted"
; isLoading
: false
}
The result of calling the usePaginatedQuery hook.
This includes:
results
- An array of the currently loaded results.isLoading
- Whether the hook is currently loading results.status
- The status of the pagination. The possible statuses are:- "LoadingFirstPage": The hook is loading the first page of results.
- "CanLoadMore": This query may have more items to fetch. Call
loadMore
to fetch another page. - "LoadingMore": We're currently loading another page of results.
- "Exhausted": We've paginated to the end of the list.
loadMore(n)
A callback to fetch more results. This will only fetch more results if the status is "CanLoadMore".
Type parameters
Name |
---|
Item |
Defined in
react/use_paginated_query.ts:441
PaginationStatus
Ƭ PaginationStatus: UsePaginatedQueryResult
<any
>["status"
]
The possible pagination statuses in UsePaginatedQueryResult.
This is a union of string literal types.
Defined in
react/use_paginated_query.ts:469
PaginatedQueryArgs
Ƭ PaginatedQueryArgs<Query
>: Expand
<BetterOmit
<FunctionArgs
<Query
>, "paginationOpts"
>>
Given a PaginatedQueryReference, get the type of the arguments
object for the query, excluding the paginationOpts
argument.
Type parameters
Name | Type |
---|---|
Query | extends PaginatedQueryReference |
Defined in
react/use_paginated_query.ts:477
PaginatedQueryItem
Ƭ PaginatedQueryItem<Query
>: FunctionReturnType
<Query
>["page"
][number
]
Given a PaginatedQueryReference, get the type of the item being paginated over.
Type parameters
Name | Type |
---|---|
Query | extends PaginatedQueryReference |
Defined in
react/use_paginated_query.ts:486
UsePaginatedQueryReturnType
Ƭ UsePaginatedQueryReturnType<Query
>: UsePaginatedQueryResult
<PaginatedQueryItem
<Query
>>
The return type of usePaginatedQuery.
Type parameters
Name | Type |
---|---|
Query | extends PaginatedQueryReference |
Defined in
react/use_paginated_query.ts:494
RequestForQueries
Ƭ RequestForQueries: Record
<string
, { query
: FunctionReference
<"query"
> ; args
: Record
<string
, Value
> }>
An object representing a request to load multiple queries.
The keys of this object are identifiers and the values are objects containing the query function and the arguments to pass to it.
This is used as an argument to useQueries.