@confect/react provides drop-in replacements for Convex’s React hooks. Each hook automatically encodes your args and decodes return values through the Effect Schemas defined in your function specs. You work with the Schema Type (decoded) values on both sides — the hooks handle the round-trip to Convex’s Encoded representation transparently.
Functions are referenced via refs (from confect/_generated/refs) instead of Convex’s api object. Each ref carries the args and returns schemas from the corresponding function spec, which is what enables the automatic encoding and decoding.
Setup
The React provider setup is the same as vanilla Convex.src/main.tsx
useQuery
Encodes args using the spec’s args schema, passes them to Convex, and decodes the result using the spec’s returns schema. Returns T | undefined (undefined while loading), matching Convex’s useQuery.
Given this spec:
confect/notes.spec.ts
{} (the Type of Schema.Struct({})) as args and returns readonly Notes.Doc["Type"][] (the Type of Schema.Array(Notes.Doc)):
useMutation
Returns a function that encodes args using the spec’s args schema, calls the Convex mutation, and decodes the result using the spec’s returns schema. Returns (args) => Promise<T>, matching Convex’s useMutation.
Given this spec:
confect/notes.spec.ts
{ text: string } (the Type of the args schema) and returns a Promise<GenericId<"notes">> (the Type of the returns schema):
useAction
Same pattern as useMutation: encodes args via the spec’s args schema, calls the Convex action, and decodes the result via the spec’s returns schema. Returns (args) => Promise<T>.
Given this spec:
confect/random.spec.ts
{} and returns a Promise<number>:
Differences from vanilla Convex hooks
| Vanilla Convex | Confect |
|---|---|
Functions referenced via api.module.fn | Functions referenced via refs |
| Args passed directly to Convex as-is | Args are schema-encoded from Type to Encoded before sending to Convex |
| Return values received directly from Convex as-is | Return values are schema-decoded from Encoded to Type before returning |