Providers
<JazzReactProvider />
is the core component that connects your React application to Jazz. It handles:
- Data Synchronization: Manages connections to peers and the Jazz cloud
- Local Storage: Persists data locally between app sessions
- Schema Types: Provides APIs for the AccountSchema
- Authentication: Connects your authentication system to Jazz
Our Chat example app provides a complete implementation of JazzReactProvider with authentication and real-time data sync.
Setting up the Provider
The <JazzReactProvider />
accepts several configuration options:
// App.tsx import {
function JazzReactProvider<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, enableSSR, }: JazzProviderProps<S>): JSX.Element
JazzReactProvider } from "jazz-tools/react"; import {MyAppAccount } from "./schema"; export function
const MyAppAccount: AccountSchema<{ root: CoMapSchema<{ todos: CoListSchema<CoMapSchema<{ title: ZodString; completed: ZodBoolean; }>>; }>; profile: CoMapSchema<...>; }>
MyApp({
function MyApp({ children }: { children: React.ReactNode; }): React.JSX.Element
children: React.ReactNode
children }: {children: React.ReactNode
children: React.type React.ReactNode = string | number | bigint | boolean | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | React.ReactPortal | Promise<...> | null | undefined
Represents all of the things React can render. Where {@link ReactElement } only represents JSX, `ReactNode` represents everything that can be rendered.ReactNode }) { return ( <function JazzReactProvider<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, enableSSR, }: JazzProviderProps<S>): JSX.Element
JazzReactProvidersync: SyncConfig
sync={{peer: `wss://${string}` | `ws://${string}`
peer: "wss://cloud.jazz.tools/?key=your-api-key",when?: "always" | "signedUp" | undefined
when: "always" // When to sync: "always", "never", or "signedUp" }}AccountSchema={
AccountSchema?: AccountSchema<{ root: CoMapSchema<{ todos: CoListSchema<CoMapSchema<{ title: ZodString; completed: ZodBoolean; }>>; }>; profile: CoMapSchema<...>; }> | undefined
MyAppAccount} > {
const MyAppAccount: AccountSchema<{ root: CoMapSchema<{ todos: CoListSchema<CoMapSchema<{ title: ZodString; completed: ZodBoolean; }>>; }>; profile: CoMapSchema<...>; }>
children: React.ReactNode
children} </function JazzReactProvider<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, enableSSR, }: JazzProviderProps<S>): JSX.Element
JazzReactProvider> ); }
Provider Options
Sync Options
The sync
property configures how your application connects to the Jazz network:
import { type
SyncConfig } from "jazz-tools"; const
type SyncConfig = { peer: `wss://${string}` | `ws://${string}`; when?: "always" | "signedUp"; } | { peer?: `wss://${string}` | `ws://${string}`; when: "never"; }
const syncConfig: SyncConfig
syncConfig:SyncConfig = { // Connection to Jazz Cloud or your own sync server
type SyncConfig = { peer: `wss://${string}` | `ws://${string}`; when?: "always" | "signedUp"; } | { peer?: `wss://${string}` | `ws://${string}`; when: "never"; }
peer: `wss://${string}` | `ws://${string}`
peer: "wss://cloud.jazz.tools/?key=your-api-key", // When to sync: "always" (default), "never", or "signedUp"when?: "always" | "signedUp" | undefined
when: "always", }
See Authentication States for more details on how the when
property affects synchronization based on authentication state.
Account Schema
The AccountSchema
property defines your application's account structure:
// app.tsx import {
MyAppAccount } from "./schema"; export function
const MyAppAccount: AccountSchema<{ root: CoMapSchema<{ todos: CoListSchema<CoMapSchema<{ title: ZodString; completed: ZodBoolean; }>>; }>; profile: CoMapSchema<...>; }>
MyApp ({
function MyApp({ children }: { children: React.ReactNode; }): React.JSX.Element
children: React.ReactNode
children }: {children: React.ReactNode
children: React.type React.ReactNode = string | number | bigint | boolean | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | React.ReactPortal | Promise<...> | null | undefined
Represents all of the things React can render. Where {@link ReactElement } only represents JSX, `ReactNode` represents everything that can be rendered.ReactNode }) { // Use in provider return ( <function JazzReactProvider<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, enableSSR, }: JazzProviderProps<S>): JSX.Element
JazzReactProvidersync: SyncConfig
sync={const syncConfig: SyncConfig
syncConfig}AccountSchema={
AccountSchema?: AccountSchema<{ root: CoMapSchema<{ todos: CoListSchema<CoMapSchema<{ title: ZodString; completed: ZodBoolean; }>>; }>; profile: CoMapSchema<...>; }> | undefined
MyAppAccount} > {
const MyAppAccount: AccountSchema<{ root: CoMapSchema<{ todos: CoListSchema<CoMapSchema<{ title: ZodString; completed: ZodBoolean; }>>; }>; profile: CoMapSchema<...>; }>
children: React.ReactNode
children} </function JazzReactProvider<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, enableSSR, }: JazzProviderProps<S>): JSX.Element
JazzReactProvider> ); }
Additional Options
The provider accepts these additional options:
// app.tsx export function
MyApp ({
function MyApp({ children }: { children: React.ReactNode; }): React.JSX.Element
children: React.ReactNode
children }: {children: React.ReactNode
children: React.type React.ReactNode = string | number | bigint | boolean | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | React.ReactPortal | Promise<...> | null | undefined
Represents all of the things React can render. Where {@link ReactElement } only represents JSX, `ReactNode` represents everything that can be rendered.ReactNode }) { return ( <function JazzReactProvider<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, enableSSR, }: JazzProviderProps<S>): JSX.Element
JazzReactProvidersync: SyncConfig
sync={const syncConfig: SyncConfig
syncConfig} // Enable guest mode for account-less accessguestMode?: boolean | undefined
guestMode={false} // Set default name for new user profilesdefaultProfileName?: string | undefined
defaultProfileName="New User" // Handle user logoutonLogOut?: (() => void) | undefined
onLogOut={() => {var console: Console
The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```console.Console.log(message?: any, ...optionalParams: any[]): void (+2 overloads)
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log("User logged out"); }} // Handle anonymous account data when user logs in to existing accountonAnonymousAccountDiscarded={(
onAnonymousAccountDiscarded?: ((anonymousAccount: Account | ({ [x: string]: any; } & Account)) => Promise<void>) | undefined
account) => {
account: Account | ({ [x: string]: any; } & Account)
var console: Console
The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```console.Console.log(message?: any, ...optionalParams: any[]): void (+2 overloads)
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log("Anonymous account discarded",account.
account: Account | ({ [x: string]: any; } & Account)
Account.id: string
id); // Migrate data here returnvar Promise: PromiseConstructor
Represents the completion of an asynchronous operationPromise.PromiseConstructor.resolve(): Promise<void> (+2 overloads)
Creates a new resolved promise.resolve(); }} > {children: React.ReactNode
children} </function JazzReactProvider<S extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, enableSSR, }: JazzProviderProps<S>): JSX.Element
JazzReactProvider> ); }
See Authentication States for more information on authentication states, guest mode, and handling anonymous accounts.
Authentication
<JazzReactProvider />
works with various authentication methods to enable users to access their data across multiple devices. For a complete guide to authentication, see our Authentication Overview.
Need Help?
If you have questions about configuring the Jazz Provider for your specific use case, join our Discord community for help.