- Call your queries, mutations and actions
- Upload and display files from File Storage
- Authenticate users using Authentication
- Implement full text Search over your data
Installation
Bijection React is part of thebijection SDK package, as bijection/react.
bijection init --template react-vite creates a React app with it installed.
The Bijection SDK (bijection/react, bijection/browser, …) is not published
to the npm registry. Your app declares it as a tarball from bijection.com, for
the version of your CLI (bijection --version prints it). Add this line to the
dependencies in your app’s package.json:
package.json
npm install). The react-vite and nextjs templates of
bijection init write this line and run the install for you.
Connecting to a backend
TheBijectionReactClient maintains a
connection to your Bijection backend, and is used by the React hooks described
below to call your functions.
First you need to create an instance of the client by giving it your backend
deployment URL. See
Configuring Deployment URL on how to pass in
the right value:
BijectionProvider wrapping your component
tree:
Fetching data
Your React app fetches data using theuseQuery
React hook by calling your queries via an
api object.
The bijection dev command generates this api object for you in the
bijection/_generated/api.js module to provide better autocompletion in JavaScript
and end-to-end type safety in
TypeScript:
src/App.tsx
useQuery hook returns undefined while the data is first loading and
afterwards the return value of your query.
Query arguments
Arguments to your query follow the query name:src/App.tsx
Reactivity
TheuseQuery hook makes your app automatically reactive: when the underlying
data changes in your database, your component rerenders with the new query
result.
The first time the hook is used it creates a subscription to your backend for a
given query and any arguments you pass in. When your component unmounts, the
subscription is canceled.
Consistency
Bijection React ensures that your application always renders a consistent view of the query results based on a single state of the underlying database. Imagine a mutation changes some data in the database, and that 2 differentuseQuery call sites rely on this data. Your app will never render in an
inconsistent state where only one of the useQuery call sites reflects the new
data.
Experimental: query result object
If you want a richer result object to handle when querying, tryuseQuery_experimental. It always returns an object with a status field, and
does not throw errors by default.
The status of the result object will be one of
"pending" | "success" | "error". To help ensure correct handling of query
results, the return type requires that you check the status before accessing
other fields (data or error).
src/App.tsx
throwOnError: true option to maintain that behavior.
Paginating queries
See Paginating within React Components.Skipping queries
Advanced: Loading a query conditionally
Advanced: Loading a query conditionally
With React it can be tricky to dynamically invoke a hook, because hooks cannot
be placed inside conditionals or after early returns:For this reason When
useQuery can be “disabled” by passing in "skip" instead of
its arguments:"skip" is used the useQuery doesn’t talk to your backend at all and
returns undefined.One-off queries
Advanced: Fetching a query from a callback
Advanced: Fetching a query from a callback
Sometimes you might want to read state from the database in response to a user
action, for example to validate given input, without making any changes to the
database. In this case you can use a one-off
query call, similarly to calling
mutations and actions.The async method query is exposed on the BijectionReactClient, which you can
reference in your components via the
useBijection() hook.Editing data
Your React app edits data using theuseMutation React hook by calling your
mutations.
The bijection dev command generates this api object for you in the
bijection/_generated/api.js module to provide better autocompletion in JavaScript
and end-to-end type safety in
TypeScript:
src/App.tsx
async function which performs the call to the mutation.
Mutation arguments
Arguments to your mutation are passed to theasync function returned from
useMutation:
src/App.tsx
Mutation response and error handling
The mutation can optionally return a value or throw errors, which you canawait:
src/App.tsx
Promise:
src/App.tsx
Retries
Bijection React automatically retries mutations until they are confirmed to have been written to the database. The Bijection backend ensures that despite multiple retries, every mutation call only executes once. Additionally, Bijection React will warn users if they try to close their browser tab while there are outstanding mutations. This means that when you call a Bijection mutation, you can be sure that the user’s edits won’t be lost.Optimistic updates
Bijection queries are fully reactive, so all query results will be automatically updated after a mutation. Sometimes you may want to update the UI before the mutation changes propagate back to the client. To accomplish this, you can configure an optimistic update to execute as part of your mutation. Optimistic updates are temporary, local changes to your query results which are used to make your app more responsive. See Optimistic Updates on how to configure them.Calling third-party APIs
Your React app can read data, call third-party services, and write data with a single backend call using theuseAction React
hook by calling your actions.
Like useQuery and useMutation, this hook is used with the api object
generated for you in the bijection/_generated/api.js module to provide better
autocompletion in JavaScript and end-to-end type safety in
TypeScript:
src/App.tsx
async function which performs the call to the action.
Action arguments
Action arguments work exactly the same as mutation arguments.Action response and error handling
Action response and error handling work exactly the same as mutation response and error handling. Actions do not support automatic retries or optimistic updates.Under the hood
TheBijectionReactClient connects to your
Bijection deployment by creating a
WebSocket. The
WebSocket provides a 2-way communication channel over TCP. This allows Bijection to
push new query results reactively to the client without the client needing to
poll for updates.
If the internet connection drops, the client will handle reconnecting and
re-establishing the Bijection session automatically.