Providers
<JazzProvider />
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 JazzProvider with authentication and real-time data sync.
Setting up the Provider
The <JazzProvider />
accepts several configuration options:
// App.tsx import {
function JazzProvider<Acc extends Account = MyAppAccount>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, }: JazzProviderProps<Acc>): JSX.Element
JazzProvider } from "jazz-react"; import {class MyAppAccount
MyAppAccount } from "./schema"; export functionMyApp({
function MyApp({ children }: { children: React.ReactNode; }): React.JSX.Element
children: React.ReactNode
children }: {children: React.ReactNode
children: React.type React.ReactNode = string | number | boolean | React.ReactElement<any, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | React.ReactPortal | 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 JazzProvider<Acc extends Account = MyAppAccount>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, }: JazzProviderProps<Acc>): JSX.Element
JazzProvidersync: 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?: AccountClass<MyAppAccount> | undefined
AccountSchema={class MyAppAccount
MyAppAccount} > {children: React.ReactNode
children} </function JazzProvider<Acc extends Account = MyAppAccount>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, }: JazzProviderProps<Acc>): JSX.Element
JazzProvider> ); } // Register the Account schema so `useAccount` returns our custom `MyAppAccount` declare module "jazz-react" { interface Register {Register.Account: MyAppAccount
Account:class MyAppAccount
MyAppAccount; } }
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 {
class MyAppAccount
MyAppAccount } from "./schema"; export functionMyApp ({
function MyApp({ children }: { children: React.ReactNode; }): React.JSX.Element
children: React.ReactNode
children }: {children: React.ReactNode
children: React.type React.ReactNode = string | number | boolean | React.ReactElement<any, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | React.ReactPortal | 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 JazzProvider<Acc extends Account = MyAppAccount>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, }: JazzProviderProps<Acc>): JSX.Element
JazzProvidersync: SyncConfig
sync={const syncConfig: SyncConfig
syncConfig}AccountSchema?: AccountClass<MyAppAccount> | undefined
AccountSchema={class MyAppAccount
MyAppAccount} > {children: React.ReactNode
children} </function JazzProvider<Acc extends Account = MyAppAccount>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, }: JazzProviderProps<Acc>): JSX.Element
JazzProvider> ); } // Register type for useAccount declare module "jazz-react" { interface Register {Register.Account: MyAppAccount
Account:class MyAppAccount
MyAppAccount; } }
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 | boolean | React.ReactElement<any, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | React.ReactPortal | 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 JazzProvider<Acc extends Account = Account>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, }: JazzProviderProps<Acc>): JSX.Element
JazzProvidersync: 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 calling `require('console')`. _**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 (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.log("User logged out"); }} // Handle anonymous account data when user logs in to existing accountonAnonymousAccountDiscarded?: ((anonymousAccount: Account) => Promise<void>) | undefined
onAnonymousAccountDiscarded={(account: Account
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 calling `require('console')`. _**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 (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.log("Anonymous account discarded",account: Account
account.Account.id: ID<Account>
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 JazzProvider<Acc extends Account = Account>({ children, guestMode, sync, storage, AccountSchema, defaultProfileName, onLogOut, logOutReplacement, onAnonymousAccountDiscarded, }: JazzProviderProps<Acc>): JSX.Element
JazzProvider> ); }
See Authentication States for more information on authentication states, guest mode, and handling anonymous accounts.
Authentication
<JazzProvider />
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.