We use cookies to collect data to improve your experience on our site. Read our [Privacy Policy](/content/privacy/index.html) to learn more.

DeclineAccept

# API Reference \- @liveblocks/react

`@liveblocks/react` provides you with [React](https://react.dev/) bindings for
our realtime collaboration APIs, built on top of WebSockets. Read our
[getting started](/content/docs/get-started/index.html) guides to learn more.

## [Suspense](/content/docs/api-reference/liveblocks-react\#Suspense/index.html)

All Liveblocks React components and hooks can be exported from two different
locations, `@liveblocks/react/suspense` and `@liveblocks/react`. This is because
Liveblocks provides two types of hooks; those that support
[React Suspense](https://react.dev/reference/react/Suspense), and those that
don’t.

```tsx
// Import the Suspense hookimport { useThreads } from "@/liveblocks/react/suspense";
// Import the regular hookimport { useThreads } from "@/liveblocks/react";
```

We recommend importing from `@liveblocks/react/suspense` and using Suspense by
default, as it often makes it easier to build your collaborative application.

Enable Suspense with createRoomContext

If you’re using the non-standard [`createRoomContext`](/content/docs/api-reference/liveblocks-react#createRoomContext/index.html)
function to build your hooks, you must
[enable suspense differently](/content/docs/api-reference/liveblocks-react#createRoomContext-Suspense/index.html).

### [Suspense hooks](/content/docs/api-reference/liveblocks-react\#Suspense-hooks/index.html)

Suspense hooks can be wrapped in [`ClientSideSuspense`](/content/docs/api-reference/liveblocks-react#ClientSideSuspsnse/index.html), which acts as a
loading spinner for any components below it. When using this, all components
below will only render once their hook contents have been loaded.

```tsx
import { ClientSideSuspense, useStorage } from "@liveblocks/react/suspense";
function App() {  <ClientSideSuspense fallback={<div>Loading…</div>}>    <Component />  </ClientSideSuspense>;}
function Component() {  // `animals` is always defined  const animals = useStorage((root) => root.animals);
  // ...}
```

Advanced hooks using the `{ ..., error, isLoading }` syntax, such as
[`useThreads`](/content/docs/api-reference/liveblocks-react#useThreads/index.html), can also use
[`ErrorBoundary`](https://github.com/bvaughn/react-error-boundary) to render an
error if the hook runs into a problem.

```tsx
import { ClientSideSuspense, useThreads } from "@liveblocks/react/suspense";import { ErrorBoundary } from "react-error-boundary";
function App() {  return (    <ErrorBoundary fallback={<div>Error</div>}>      <ClientSideSuspense fallback={<div>Loading…</div>}>        <Component />      </ClientSideSuspense>    </ErrorBoundary>  );}
function Component() {  // `threads` is always defined  const { threads } = useThreads();
  // ...}
```

An advantage of Suspense hooks is that you can have multiple different hooks in
your tree, and you only need a single `ClientSideSuspense` component to render a
loading spinner for all of them.

### [Regular hooks](/content/docs/api-reference/liveblocks-react\#Regular-hooks/index.html)

Regular hooks often return `null` whilst a component is loading, and you must
check for this to render a loading spinner.

```tsx
import { useStorage } from "@liveblocks/react";
function Component() {  // `animals` is `null` when loading  const animals = useStorage((root) => root.animals);
  if (!animals) {    return <div>Loading…</div>;  }
  // ...}
```

Advanced hooks using the `{ ..., error, isLoading }` syntax, such as
[`useThreads`](/content/docs/api-reference/liveblocks-react#useThreads/index.html), require you to make sure there isn’t a problem before using
the data.

```tsx
import { useThreads } from "@liveblocks/react";
function Component() {  // Check for `error` and `isLoading` before `threads` is defined  const { threads, error, isLoading } = useThreads();
  if (error) {    return <div>Error</div>;  }
  if (isLoading) {    return <div>Loading…</div>;  }
  // ...}
```

### [ClientSideSuspense](/content/docs/api-reference/liveblocks-react\#ClientSideSuspense/index.html)

Liveblocks provides a component named `ClientSideSuspense` which works as a
replacement for `Suspense`. This is helpful as our Suspense hooks will throw an
error when they’re run on the server, and this component avoids this issue by
always rendering the `fallback` on the server.

```tsx
import { ClientSideSuspense } from "@liveblocks/react/suspense";
function Page() {  return (    <LiveblocksProvider authEndpoint="/api/liveblocks-auth">      <RoomProvider id="my-room-id">        <ClientSideSuspense fallback={<div>Loading…</div>}>          <App />        </ClientSideSuspense>      </RoomProvider>    </LiveblocksProvider>  );}
```

#### [Loading spinners](/content/docs/api-reference/liveblocks-react\#Loading-spinners/index.html)

Instead of wrapping your entire Liveblocks application inside a single
`ClientSideSuspense` component, you can use multiple of these components in
different parts of your application, and each will work as a loading fallback
for any components further down your tree.

```tsx
import { ClientSideSuspense } from "@liveblocks/react/suspense";
function Page() {  return (    <LiveblocksProvider authEndpoint="/api/liveblocks-auth">      <RoomProvider id="my-room-id">        <header>My title</header>
        <main>          <ClientSideSuspense fallback={<div>Loading…</div>}>            <Canvas />          </ClientSideSuspense>        </main>
        <aside>          <ClientSideSuspense fallback={<div>Loading…</div>}>            <LiveAvatars />          </ClientSideSuspense>        </aside>      </RoomProvider>    </LiveblocksProvider>  );}
```

This is a great way to build a static skeleton around your dynamic collaborative
application.

## [Liveblocks](/content/docs/api-reference/liveblocks-react\#Liveblocks/index.html)

### [LiveblocksProvider](/content/docs/api-reference/liveblocks-react\#LiveblocksProvider/index.html)

Sets up a client for connecting to Liveblocks, and is the recommended way to do
this for React apps. You must define either `authEndpoint` or `publicApiKey`.
Resolver functions should be placed inside here, and a number of other options
are available, which correspond with those passed to [`createClient`](/content/docs/api-reference/liveblocks-client#createClient/index.html). Unlike
[`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html), `LiveblocksProvider` doesn’t call Liveblocks servers when
mounted, and it should be placed higher in your app’s component tree.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider    // publicApiKey=""    // authEndpoint="/api/liveblocks-auth"    >      {/* children */}    </LiveblocksProvider>  );}
```

All LiveblocksProvider props

Props

- authEndpointSee full type

The URL of your back end’s [authentication endpoint](/content/docs/authentication/index.html)
as a string, or an async callback function that returns a Liveblocks token
result. The result is cached by the Liveblocks client, and called fresh only
when necessary, so never cache it yourself. Either `authEndpoint` or
`publicApiKey` are required. Learn more about [using a URL\\
string](/content/docs/api-reference/liveblocks-react#LiveblocksProviderAuthEndpoint/index.html) and [using a\\
callback](/content/docs/api-reference/liveblocks-react#LiveblocksProviderCallback/index.html).

- publicApiKeystring

The public API key taken from your project’s
[dashboard](/content/dashboard/apikeys/index.html). Generally not recommended for production
use. Either `authEndpoint` or `publicApiKey` are required. [Learn\\
more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderPublicKey/index.html).

- throttlenumberDefault is100

The throttle time between WebSocket messages in milliseconds, a number
between `16` and `1000` is allowed. Using `16` means your app will update 60
times per second. [Learn more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderThrottle/index.html).

- preventUnsavedChangesbooleanDefault isfalse

When set, navigating away from the current page is prevented while
Liveblocks is still synchronizing local changes. [Learn\\
more](/content/docs/api-reference/liveblocks-react#prevent-users-losing-unsaved-changes/index.html).

- lostConnectionTimeoutnumberDefault is5000

After a user disconnects, the time in milliseconds before a
[`"lost-connection"`](/content/docs/api-reference/liveblocks-client#Room.subscribe.lost-connection/index.html)
event is fired. [Learn more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderLostConnectionTimeout/index.html).

- backgroundKeepAliveTimeoutnumber

The time before an inactive WebSocket connection is disconnected. This is
disabled by default, but setting a number will activate it. [Learn\\
more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderBackgroundKeepAliveTimeout/index.html).

- resolveUsersSee full type

A function that resolves user information in
[Comments](/content/docs/ready-made-features/comments/index.html), [Text\\
Editor](/content/docs/ready-made-features/text-editor/index.html), and
[Notifications](/content/docs/ready-made-features/notifications/index.html). Return an array of
`UserMeta["info"]` objects in the same order they arrived. [Learn\\
more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderResolveUsers/index.html).

- resolveRoomsInfoSee full type

A function that resolves room information in
[Notifications](/content/docs/ready-made-features/notifications/index.html). Return an array of
`RoomInfo` objects in the same order they arrived. [Learn\\
more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderResolveRoomsInfo/index.html).

- resolveGroupsInfoSee full type

A function that resolves group information in
[Comments](/content/docs/ready-made-features/comments/index.html) and [Text\\
Editor](/content/docs/ready-made-features/text-editor/index.html). Return an array of
`GroupInfo` objects in the same order they arrived. [Learn\\
more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderResolveGroupsInfo/index.html).

- resolveMentionSuggestionsSee full type

A function that resolves mention suggestions in
[Comments](/content/docs/ready-made-features/comments/index.html) and [Text\\
Editor](/content/docs/ready-made-features/text-editor/index.html). Return an array of user IDs
or mention objects. [Learn\\
more](/content/docs/api-reference/liveblocks-react#LiveblocksProviderResolveMentionSuggestions/index.html).

- polyfills

Place polyfills for `atob`, `fetch`, and `WebSocket` inside here. Useful
when using a non-browser environment, such as
[Node.js](/content/docs/api-reference/liveblocks-react#LiveblocksProviderNode/index.html) or [React\\
Native](/content/docs/api-reference/liveblocks-react#LiveblocksProviderReactNative/index.html).

- badgeLocationDefault is"bottom-right"

The location of the "Powered by Liveblocks" badge. Can be set to either
`"top-right"`, `"bottom-right"`, `"bottom-left"`, or `"top-left"`. [Learn\\
more](/content/docs/api-reference/liveblocks-react#Powered-by-Liveblocks-branding/index.html).

- unstable\_streamDatabooleanDefault isfalseDeprecated

Deprecated. For new rooms, use [`engine: 2`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html) instead. Engine
2 rooms have native support for streaming. This flag will be removed in a
future version, but will continue to work for existing engine 1 rooms for
now. [Learn more](/content/docs/guides/the-new-storage-engine-and-its-benefits/index.html).

#### [LiveblocksProvider with public key](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderPublicKey/index.html)

When creating a client with a public key, you don’t need to set up an
authorization endpoint. We only recommend using a public key when prototyping,
or on public landing pages, as it makes it possible for end users to access any
room’s data. You should instead use an
[auth endpoint](/content/docs/api-reference/liveblocks-react#LiveblocksProviderAuthEndpoint/index.html).

[Sign in](/content/dashboard/apikeys/index.html) to get your own API keys.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider publicApiKey={"pk_prod_xxxxxxxxxx…xxxxxxxxpk_prod_xxxxxxxxxxxxxxxxxxxxxxxx"}>      {/* children */}    </LiveblocksProvider>  );}
```

#### [LiveblocksProvider with auth endpoint](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderAuthEndpoint/index.html)

If you are not using a public key, you need to set up your own `authEndpoint`.
Please refer to our [Authentication guide](/content/docs/authentication/index.html).

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider authEndpoint="/api/liveblocks-auth">      {/* children */}    </LiveblocksProvider>  );}
```

Don't cache tokens

Never cache your authentication endpoint, as your client will not function
correctly. The Liveblocks client will cache results for you, only making
requests to the endpoint if necessary, such as when the token has expired.

#### [LiveblocksProvider with auth endpoint callback](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderCallback/index.html)

If you need to add additional headers or use your own function to call your
endpoint, `authEndpoint` can be provided as a custom callback. You should return
the token created with
[`Liveblocks.prepareSession`](/content/docs/api-reference/liveblocks-node#access-tokens/index.html)
or [`liveblocks.identifyUser`](/content/docs/api-reference/liveblocks-node#id-tokens/index.html),
learn more in [authentication guide](/content/docs/rooms/authentication/index.html).

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      authEndpoint={async (room) => {        // Fetch your authentication endpoint and retrieve your access or ID token        // ...
        return { token: "..." };      }}    >      {/* children */}    </LiveblocksProvider>  );}
```

`room` is the room ID that the user is connecting to. When using
[Notifications](/content/docs/ready-made-features/comments/email-notifications/index.html), `room`
can be `undefined`, as the client is requesting a token that grants access to
multiple rooms, rather than a specific room.

##### [Fetch your endpoint](/content/docs/api-reference/liveblocks-react\#Fetch-your-endpoint/index.html)

Here’s an example of fetching your API endpoint at `/api/liveblocks-auth` within
the callback.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      authEndpoint={async (room) => {        const response = await fetch("/api/liveblocks-auth", {          method: "POST",          headers: {            Authentication: "<your own headers here>",            "Content-Type": "application/json",          },          // Don't forget to pass `room` down. Note that it          // can be undefined when using Notifications.          body: JSON.stringify({ room }),        });        return await response.json();      }}    >      {/* children */}    </LiveblocksProvider>  );}
```

##### [Token details](/content/docs/api-reference/liveblocks-react\#Token-details/index.html)

You should return the token created with
[`Liveblocks.prepareSession`](/content/docs/api-reference/liveblocks-node#access-tokens/index.html)
or [`liveblocks.identifyUser`](/content/docs/api-reference/liveblocks-node#id-tokens/index.html).
These are the values the functions can return.

1. A valid token, it returns a `{ "token": "..." }` shaped response.
2. A token that explicitly forbids access, it returns an
`{ "error": "forbidden", "reason": "..." }` shaped response. If this is
returned, the client will disconnect and won't keep trying to authorize.

Any other error will be treated as an unexpected error, after which the client
will retry the request until it receives either 1. or 2.

#### [WebSocket throttle](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderThrottle/index.html)

By default, the client throttles the WebSocket messages sent to one every 100
milliseconds, which translates to 10 updates per second. It’s possible to
override that configuration with the `throttle` option with a value between `16`
and `1000` milliseconds.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      throttle={16}
      // Other options      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

This option is helpful for smoothing out realtime animations in your
application, as you can effectively increase the framerate without using any
interpolation. Here are some examples with their approximate frames per second
(FPS) values.

```ts
throttle:  16, // 60 FPSthrottle:  32, // 30 FPSthrottle: 200, //  5 FPS
```

#### [Prevent users losing unsaved changes](/content/docs/api-reference/liveblocks-react\#prevent-users-losing-unsaved-changes/index.html)

Liveblocks usually synchronizes milliseconds after a local change, but if a user
immediately closes their tab, or if they have a slow connection, it may take
longer for changes to synchronize. Enabling `preventUnsavedChanges` will stop
tabs with unsaved changes closing, by opening a dialog that warns users. In
usual circumstances, it will very rarely trigger.

```tsx
function Page() {  return (    <LiveblocksProvider      preventUnsavedChanges
      // Other options      // ...    >      ...    </LiveblocksProvider>  );}
```

More specifically, this option triggers when:

- There are unsaved changes after calling any hooks or methods, in all of our
products.
- There are unsaved changes in a
[Text Editor](/content/docs/ready-made-features/text-editor/index.html).
- There’s an unsubmitted comment in the
[Composer](/content/docs/api-reference/liveblocks-react-ui#Composer/index.html).
- The user has made changes and is currently offline.

Internally, this option uses the
[beforeunload event](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event).

#### [Lost connection timeout](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderLostConnectionTimeout/index.html)

If you’re connected to a room and briefly lose connection, Liveblocks will
reconnect automatically and quickly. However, if reconnecting takes longer than
usual, for example if your network is offline, then the room will emit an event
informing you about this.

How quickly this event is triggered can be configured with the
`lostConnectionTimeout` setting, and it takes a number in milliseconds.
`lostConnectionTimeout` can be set between `1000` and `30000` milliseconds. The
default is `5000`, or 5 seconds.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      lostConnectionTimeout={5000}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

You can listen to the event with [`useLostConnectionListener`](/content/docs/api-reference/liveblocks-react#useLostConnectionListener/index.html). Note that this
also affects when `others` are reset to an empty array after a disconnection.
This helps prevent temporary flashes in your application as a user quickly
disconnects and reconnects. For a demonstration of this behavior, see our
[connection status example](/content/examples/connection-status/nextjs/index.html).

#### [Background keep-alive timeout](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderBackgroundKeepAliveTimeout/index.html)

By default, Liveblocks applications will maintain an active WebSocket connection
to the Liveblocks servers, even when running in a browser tab that’s in the
background. However, if you’d prefer for background tabs to disconnect after a
period of inactivity, then you can use `backgroundKeepAliveTimeout`.

When `backgroundKeepAliveTimeout` is specified, the client will automatically
disconnect applications that have been in an unfocused background tab for _at_
_least_ the specified time. When the browser tab is refocused, the client will
immediately reconnect to the room and synchronize the document.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      // Disconnect users after 15 minutes of inactivity      backgroundKeepAliveTimeout={15 * 60 * 1000}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

`backgroundKeepAliveTimeout` accepts a number in milliseconds—we advise using a
value of at least a few minutes, to avoid unnecessary disconnections.

#### [resolveUsers](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderResolveUsers/index.html)

[Comments](/content/docs/ready-made-features/comments/index.html) and
[Text Editor](/content/docs/ready-made-features/text-editor/index.html) store user IDs in its
system, but no other user information. To display user information in Comments,
Text Editor, and Notifications components, such as a user’s name or avatar, you
need to resolve these IDs into user objects. This function receives a list of
user IDs and you should return a list of user objects of the same size, in the
same order.

User IDs are automatically resolved in batches with a maximum of 50 users per
batch to optimize performance and prevent overwhelming your user resolution
function.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      resolveUsers={async ({ userIds }) => {        const usersData = await __getUsersFromDB__(userIds);
        return usersData.map((userData) => ({          name: userData.name,          avatar: userData.avatar.src,        }));      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

The name and avatar you return are rendered in
[`Thread`](/content/docs/api-reference/liveblocks-react-ui#Thread/index.html) components.

##### [User objects](/content/docs/api-reference/liveblocks-react\#User-objects/index.html)

The user objects returned by the resolver function take the shape of
`UserMeta["info"]`, which contains `name` and `avatar` by default. These two
values are optional, though if you’re using the
[Comments default components](/content/docs/api-reference/liveblocks-react-ui#Components/index.html),
they are necessary. Here’s an example of `userIds` and the exact values
returned.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      resolveUsers={async ({ userIds }) => {        // ["marc@example.com", "nimesh@example.com"];        console.log(userIds);
        return [          { name: "Marc", avatar: "https://example.com/marc.png" },          { name: "Nimesh", avatar: "https://example.com/nimesh.png" },        ];      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

You can also return custom information, for example, a user’s `color`:

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      resolveUsers={async ({ userIds }) => {        // ["marc@example.com"];        console.log(userIds);
        return [          {            name: "Marc",            avatar: "https://example.com/marc.png",            color: "purple",          },        ];      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

##### [Accessing user data](/content/docs/api-reference/liveblocks-react\#Accessing-user-data/index.html)

You can access any values set within `resolveUsers` with the
[`useUser`](/content/docs/api-reference/liveblocks-react#useUser/index.html) hook.

```tsx
import { useUser } from "@liveblocks/react/suspense";
function Component() {  const user = useUser("marc@example.com");
  // { name: "Marc", avatar: "https://...", ... }  console.log(user);}
```

#### [resolveRoomsInfo](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderResolveRoomsInfo/index.html)

When using
[Notifications](/content/docs/ready-made-features/comments/email-notifications/index.html) with
[Comments](/content/docs/ready-made-features/comments/index.html), room IDs will be used to
contextualize notifications (e.g. “Chris mentioned you in _room-id_”) in the
[`InboxNotification`](/content/docs/api-reference/liveblocks-react-ui#InboxNotification/index.html)
component. To replace room IDs with more fitting names (e.g. document names,
“Chris mentioned you in _Document A_”), you can provide a resolver function to
the `resolveRoomsInfo` option in [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html).

This resolver function will receive a list of room IDs and should return a list
of room info objects of the same size and in the same order.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      resolveRoomsInfo={async ({ roomIds }) => {        const documentsData = await __getDocumentsFromDB__(roomIds);
        return documentsData.map((documentData) => ({          name: documentData.name,          // url: documentData.url,        }));      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

In addition to the room’s name, you can also provide a room’s URL as the `url`
property. If you do so, the
[`InboxNotification`](/content/docs/api-reference/liveblocks-react-ui#InboxNotification/index.html)
component will automatically use it. It’s possible to use an inbox
notification’s `roomId` property to construct a room’s URL directly in React and
set it on
[`InboxNotification`](/content/docs/api-reference/liveblocks-react-ui#InboxNotification/index.html)
via `href`, but the room ID might not be enough for you to construct the URL,
you might need to call your backend for example. In that case, providing it via
`resolveRoomsInfo` is the preferred way.

#### [resolveGroupsInfo](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderResolveGroupsInfo/index.html)

When using group mentions with [Comments](/content/docs/ready-made-features/comments/index.html)
and [Text Editor](/content/docs/ready-made-features/text-editor/index.html), group IDs will be used
instead of user IDs. Similarly to
[`resolveUsers`](/content/docs/api-reference/liveblocks-react#LiveblocksProviderResolveUsers/index.html), you can provide a resolver
function to the `resolveGroupsInfo` option in
[`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html) to assign information like names and
avatars to group IDs.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      resolveGroupsInfo={async ({ groupIds }) => {        const groupsData = await __getGroupsFromDB__(groupIds);
        return groupsData.map((groupData) => ({          name: groupData.name,          avatar: groupData.avatar.src,          // description: groupData.description,        }));      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

##### [Accessing group info](/content/docs/api-reference/liveblocks-react\#Accessing-group-info/index.html)

You can access any values set within `resolveGroupsInfo` with the
[`useGroupInfo`](/content/docs/api-reference/liveblocks-react#useGroupInfo/index.html) hook.

```tsx
import { useGroupInfo } from "@liveblocks/react/suspense";
function Component() {  const group = useGroupInfo("group-engineering");
  // { name: "Engineering", avatar: "https://...", ... }  console.log(group);}
```

#### [resolveMentionSuggestions](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderResolveMentionSuggestions/index.html)

To enable creating mentions in [Comments](/content/docs/ready-made-features/comments/index.html)
and [Text Editor](/content/docs/ready-made-features/text-editor/index.html), you can provide a
resolver function to the `resolveMentionSuggestions` option in
[`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html). These mentions will be displayed in
the [`Composer`](/content/docs/api-reference/liveblocks-react-ui#Composer/index.html) component and
in text editors.

This resolver function will receive the mention currently being typed (e.g. when
writing “@jane”, `text` will be `jane`) and should return a list of user IDs
matching that text. This function will be called every time the text changes but
with some debouncing.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      resolveMentionSuggestions={async ({ text, roomId }) => {        const workspaceUsers = await __getWorkspaceUsersFromDB__(roomId);
        if (!text) {          // Show all workspace users by default          return __getUserIds__(workspaceUsers);        } else {          const matchingUsers = __findUsers__(workspaceUsers, text);          return __getUserIds__(matchingUsers);        }      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

##### [Group mentions](/content/docs/api-reference/liveblocks-react\#Group-mentions/index.html)

To support group mentions in [Comments](/content/docs/ready-made-features/comments/index.html) and
[Text Editor](/content/docs/ready-made-features/text-editor/index.html), you can return a list of
mention objects instead of user IDs to suggest a mix of user and group mentions.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() {  return (    <LiveblocksProvider      resolveMentionSuggestions={async ({ text, roomId }) => {        const dbUsers = await __findUsersFromDB__(roomId);        const dbGroups = await __findGroupsFromDB__(roomId);
        // Show groups and users matching the text being typed        return [          ...dbGroups.map((group) => ({            kind: "group",            id: group.id,          })),          ...dbUsers.map((user) => ({            kind: "user",            id: user.id,          })),        ];      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

The mention objects specify which kind of mention it is, the ID to mention (user
ID or group ID), etc.

```tsx
// A user mention suggestion{  kind: "user",  id: "user-1",}
// A group mention suggestion{  kind: "group",  id: "group-1",}
// A group mention suggestion with fixed group members// When using fixed group members via `userIds`, they will take precedence// if the group ID exists on Liveblocks.{  kind: "group",  id: "here",  userIds: ["user-1", "user-2"],}
```

#### [LiveblocksProvider for Node.js](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderNode/index.html)

To use `@liveblocks/client` in Node.js, you need to provide [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) and
[`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) polyfills. As polyfills, we recommend installing [`ws`](https://www.npmjs.com/package/ws) and
[`node-fetch`](https://npmjs.com/package/node-fetch).

Terminal

npm

```text
npm install ws node-fetch
```

Then, pass them to the `LiveblocksProvider` polyfill option as below.

```tsx
import { LiveblocksProvider } from "@liveblocks/react/suspense";import fetch from "node-fetch";import WebSocket from "ws";
function App() {  return (    <LiveblocksProvider      polyfills={{        fetch,        WebSocket,      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

Note that `node-fetch` v3+
[does not support CommonJS](https://github.com/node-fetch/node-fetch/blob/main/docs/v3-UPGRADE-GUIDE.md#converted-to-es-module).
If you are using CommonJS, downgrade `node-fetch` to v2.

#### [LiveblocksProvider for React Native](/content/docs/api-reference/liveblocks-react\#LiveblocksProviderReactNative/index.html)

To use `@liveblocks/client` with [React Native](https://reactnative.dev/), you
need to add an [`atob`](https://developer.mozilla.org/en-US/docs/Web/API/atob) polyfill. As a polyfill, we recommend installing
[`base-64`](https://www.npmjs.com/package/base-64).

Terminal

npm

```text
npm install base-64
```

Then you can pass the `decode` function to our `atob` polyfill option when you
create the client.

```ts
import { LiveblocksProvider } from "@liveblocks/react/suspense";import { decode } from "base-64";
function App() {  return (    <LiveblocksProvider      polyfills={{        atob: decode,      }}
      // Other props      // ...    >      {/* children */}    </LiveblocksProvider>  );}
```

#### [Powered by Liveblocks branding](/content/docs/api-reference/liveblocks-react\#Powered-by-Liveblocks-branding/index.html)

By default, Liveblocks displays a "Powered by Liveblocks" badge in your
application. You can adjust the position of the badge by setting the
`badgeLocation` property on `LiveblocksProvider`.

Set badge location

```tsx
// "top-right", "bottom-right", "bottom-left", "top-left"<LiveblocksProvider badgeLocation="bottom-right">  <App /></LiveblocksProvider>
```

If you wish to remove remove the badge entirely, you can do so by following
these steps:

1. In the Liveblocks dashboard, navigate to your
[team’s settings](/content/dashboard/settings/index.html).
2. Under **General**, toggle on the remove "Powered by Liveblocks" branding
option.

Removing the "Powered by Liveblocks" badge

Removing the "Powered by Liveblocks" badge on your projects requires a
[paid plan](/content/pricing/index.html). See the [pricing page](/content/pricing/index.html) for more information.

### [createLiveblocksContext](/content/docs/api-reference/liveblocks-react\#createLiveblocksContext/index.html)

Not recommended

This used to be the default way to start your app, but now it’s recommended for
advanced usage only. We generally recommend using [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html) and
following [typing your data with the Liveblocks interface](/content/docs/api-reference/liveblocks-react#Typing-your-data/index.html),
unless you need to define multiple room types in your application.

Creates a [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html) and a set of typed hooks. Note that any
`LiveblocksProvider` created in this way takes no props, because it uses
settings from the `client` instead. We recommend using it in
`liveblocks.config.ts` and re-exporting your typed hooks like below.

While [`createRoomContext`](/content/docs/api-reference/liveblocks-react#createRoomContext/index.html) offers APIs for interacting with
rooms (e.g. Presence, Storage, and Comments),
[`createLiveblocksContext`](/content/docs/api-reference/liveblocks-react#createLiveblocksContext/index.html) offers APIs for
interacting with Liveblocks features that are not tied to a specific room (e.g.
Notifications).

liveblocks.config.ts

```tsx
import { createClient } from "@liveblocks/client";import { createRoomContext, createLiveblocksContext } from "@liveblocks/react";
const client = createClient({  // publicApiKey: "",  // authEndpoint: "/api/liveblocks-auth",  // throttle: 100,});
// ...
export const { RoomProvider } = createRoomContext(client);
export const {  LiveblocksProvider,  useInboxNotifications,
  // Other hooks  // ...} = createLiveblocksContext(client);
```

### [useClientLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useClient/index.html)

Returns the [`client`](/content/docs/api-reference/liveblocks-client#createClient/index.html) of
the nearest [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html) above in the React component tree.

```ts
import { useClient } from "@liveblocks/react/suspense";
const client = useClient();
```

Arguments

_None_

Returns

- clientClient

The [Liveblocks client](/content/docs/api-reference/liveblocks-client#createClient/index.html)
instance from the nearest [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html).

### [useErrorListenerLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useErrorListener/index.html)

Listen to potential Liveblocks errors. Examples of errors include room
connection errors, errors creating threads, and errors deleting notifications.
Each error has a `message` string, and a `context` object which has different
values for each error type. `context` always contains an error `type` and
`roomId`.

```ts
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  // { message: "You don't have access to this room", context: { ... }}  console.error(error);});
```

There are many different errors, and each can be handled separately by checking
the value of `error.context.type`. Below we’ve listed each error and the context
it provides.

All error types

Arguments

- callback(error: LiveblocksError) => void

A callback function that will be called when a Liveblocks error occurs. The
error object contains a message and context with error-specific information.

## [AI Copilots](/content/docs/api-reference/liveblocks-react\#AI-Copilots/index.html)

### [useAiChatsLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useAiChats/index.html)

Returns a paginated list of AI chats created by the current user. Initially
fetches the latest 50 chats.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useAiChats } from "@liveblocks/react";
const { chats, error, isLoading } = useAiChats();
```

Options

- queryAiChatsQuery

Optional query to filter chats by metadata values or absence of metadata
keys. [Learn more](/content/docs/api-reference/liveblocks-react#useAiChats-query/index.html)

Returns

- chatsAiChat\[\] \| undefined

An array of AI chats created by the current user.

- isLoadingboolean

Whether the chats are currently being loaded.

- errorError \| undefined

Any error that occurred while loading the chats. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useAiChats-error-handling/index.html).

- hasFetchedAllboolean

Whether all available chats have been fetched. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useAiChats-pagination/index.html).

- fetchMore() =\> void

A function to fetch more chats. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useAiChats-pagination/index.html).

- isFetchingMoreboolean

Whether more chats are currently being fetched. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useAiChats-pagination/index.html).

- fetchMoreErrorError \| undefined

Any error that occurred while fetching more chats. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useAiChats-pagination/index.html).

##### [List the user's chats and switch between them](/content/docs/api-reference/liveblocks-react\#List-the-user's-chats-and-switch-between-them/index.html)

You can use the [`AiChat`](/content/docs/api-reference/liveblocks-react-ui#AiChat/index.html)
component alongside the hook to create an AI chat switcher. Below, each button
displays the chat's automatically generated title, and chats can be deleted with
[`useDeleteAiChat`](/content/docs/api-reference/liveblocks-react#useDeleteAiChat/index.html).

```tsx
import { useState } from "react";import { AiChat } from "@liveblocks/react-ui";import { useAiChats } from "@liveblocks/react";
function Chats() {  const { chats, error, isLoading } = useAiChats();  const [chatId, setChatId] = useState();  const deleteChat = useDeleteAiChat();
  if (isLoading) {    return <div>Loading...</div>;  }
  if (error) {    return <div>Error: {error.message}</div>;  }
  return (    <div style={{ display: "flex" }}>      <ul>        {chats.map((chat) => (          <li key={chat.id}>            <button onClick={() => setChatId(chat.id)}>              {chat.title || "Untitled"}            </button>            <button onClick={() => deleteChat(chat.id)}>❌</button>          </li>        ))}      </ul>      <AiChat chatId={chatId} />    </div>  );}
```

#### [Querying chats](/content/docs/api-reference/liveblocks-react\#useAiChats-query/index.html)

It’s possible to return chats that match a certain query with the `query`
option. You can filter by metadata values, or by the absence of a metadata key.
Returned chats must match the entire query.

```tsx
import { useAiChats } from "@liveblocks/react";
// Filter by metadata values and by absence of a keyconst { chats } = useAiChats({  query: {    metadata: {      // Match chats that are of type 'temporary'      type: "temporary",
      // Match chats that have all of these tags      tag: ["urgent", "billing"],
      // Match chats where the "archived" key does not exist      archived: null,    },  },});
```

#### [Pagination](/content/docs/api-reference/liveblocks-react\#useAiChats-pagination/index.html)

By default, the `useAiChats` hook returns up to 50 chats. To fetch more, the
hook provides additional fields for pagination, similar to [`useThreads`](/content/docs/api-reference/liveblocks-react#useThreads/index.html).

```tsx
import { useAiChats } from "@liveblocks/react";
const {  chats,  isLoading,  error,
  hasFetchedAll,  fetchMore,  isFetchingMore,  fetchMoreError,} = useAiChats();
```

- `hasFetchedAll` indicates whether all available AI chats have been fetched.
- `fetchMore` loads up to 50 more AI chats, and is always safe to call.
- `isFetchingMore` indicates whether more AI chats are being fetched.
- `fetchMoreError` returns error statuses resulting from fetching more.

##### [Pagination example](/content/docs/api-reference/liveblocks-react\#useAiChats-pagination-example/index.html)

The following example demonstrates how to use the `fetchMore` function to
implement a “Load More” button, which fetches additional AI chats when clicked.
The button is disabled while fetching is in progress.

```tsx
import { AiChat } from "@liveblocks/react-ui";import { useAiChats } from "@liveblocks/react";
function Inbox() {  const { chats, hasFetchedAll, fetchMore, isFetchingMore } = useAiChats();
  return (    <div>      {chats.map((chat) => (        <AiChat key={chat.id} chatId={chat.id} />      ))}      {hasFetchedAll ? (        <div>🎉 All chats loaded!</div>      ) : (        <button disabled={isFetchingMore} onClick={fetchMore}>          Load more        </button>      )}    </div>  );}
```

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useAiChats-error-handling/index.html)

Error handling is another important aspect to consider when using the
`useAiChats` hook. The `error` and `fetchMoreError` fields provide information
about any errors that occurred during the initial fetch or subsequent fetch
operations, respectively. You can use these fields to display appropriate error
messages to the user and implement retry mechanisms if needed.

The following example shows how to display error messages for both initial
loading errors and errors that occur when fetching more inbox notifications.

```tsx
import { AiChat } from "@liveblocks/react-ui";import { useAiChats } from "@liveblocks/react";
function Inbox() {  const { chats, error, fetchMore, fetchMoreError } = useAiChats();
  // Handle error if the initial load failed.  // The `error` field is not returned by the Suspense hook as the error is thrown to nearest ErrorBoundary  if (error) {    return (      <div>        <p>Error loading AI chats: {error.message}</p>      </div>    );  }
  return (    <div>      {chats.map((chat) => (        <AiChat key={chat.id} chatId={chat.id} />      ))}
      {fetchMoreError && (        <div>          <p>Error loading more AI chats: {fetchMoreError.message}</p>          <button onClick={fetchMore}>Retry</button>        </div>      )}    </div>  );}
```

### [useAiChat](/content/docs/api-reference/liveblocks-react\#useAiChat/index.html)

Returns information about an AI chat, for example its title and metadata. Titles
are automatically generated from the content of the first user message in a
chat, and the AI’s response.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useAiChat } from "@liveblocks/react";
const { chat, error, isLoading } = useAiChat("my-chat-id");
```

Arguments

- chatIdstring

The ID of the AI chat to retrieve information for.

Returns

- chatAiChat \| undefined

The AI chat object containing title, metadata, and other properties.

- isLoadingboolean

Whether the chat information is currently being loaded.

- errorError \| undefined

Any error that occurred while loading the chat information.

#### [Displaying a default title](/content/docs/api-reference/liveblocks-react\#Displaying-a-default-title/index.html)

If `chat.title` is `undefined` after an `isLoading` check, that means the title
has not been set yet. You can display a default title in this case, and the
title will be displayed once generated.

```tsx
import { useAiChat } from "@liveblocks/react";
function ChatTitle() {  const { chat, error, isLoading } = useAiChat("my-chat-id");
  if (isLoading || error) {    return null;  }
  return <div>{chat.title || "Untitled chat"}</div>;}
```

### [useCreateAiChat](/content/docs/api-reference/liveblocks-react\#useCreateAiChat/index.html)

Returns a function that creates an AI chat.

```tsx
import { useCreateAiChat } from "@liveblocks/react/suspense";
const createAiChat = useCreateAiChat();createAiChat("my-ai-chat");
```

Arguments

_None_

Returns

- createAiChat(chatIdOrOptions: string \| CreateAiChatOptions) => AiChat

A function that creates an AI chat. Can be called with either a string ID or
an options object containing `id`, optional `title`, and optional
`metadata`.

#### [Create a chat with a custom title and metadata](/content/docs/api-reference/liveblocks-react\#Create-a-chat-with-a-custom-title-and-metadata/index.html)

You can optionally set a `title` with `useCreateAiChat`, which prevents the AI
auto-generating a title from the first messages. Additionally, you can choose to
set custom `metadata` for the chat, strings or arrays of strings.

```tsx
import { useCreateAiChat } from "@liveblocks/react/suspense";
const createAiChat = useCreateAiChat();createAiChat({  id: "my-ai-chat",  title: "My AI Chat",  metadata: {    color: "red",    tags: ["product", "engineering"],  },});
```

### [useDeleteAiChat](/content/docs/api-reference/liveblocks-react\#useDeleteAiChat/index.html)

Returns a function that deletes an AI chat by its ID. Use in conjunction with
[`useAiChats`](/content/docs/api-reference/liveblocks-react#useAiChats/index.html) to
[loop through each chat](/content/docs/api-reference/liveblocks-react#List-the-user's-chats-and-switch-between-them/index.html)
and add a delete button.

```tsx
import { useDeleteAiChat } from "@liveblocks/react/suspense";
const deleteAiChat = useDeleteAiChat();deleteAiChat("my-chat-id");
```

Arguments

_None_

Returns

- deleteAiChat(chatId: string) => void

A function that deletes an AI chat by its ID.

### [useSendAiMessage](/content/docs/api-reference/liveblocks-react\#useSendAiMessage/index.html)

Returns a function that sends a message to an AI chat, identified by its ID.
Useful for
[creating suggestions in empty inside chats](/content/docs/api-reference/liveblocks-react-ui#AiChat-placeholder/index.html)
and sending messages on behalf of the user.

```tsx
import { useSendAiMessage } from "@liveblocks/react";
const sendAiMessage = useSendAiMessage("my-chat-id");sendAiMessage("Hello!");
```

Remember to [set your copilot ID](/content/docs/api-reference/liveblocks-react/index.html) otherwise the default copilot will be used.

```tsx
const sendAiMessage = useSendAiMessage("my-chat-id", {  copilotId: "co_h7GBa3...",});
```

Arguments

- chatIdstring

Optional. The ID of the AI chat to send messages to. Can also be provided
when calling the returned function.

- optionsobject

Optional configuration object.

- options.copilotIdstring

Optional. The ID of the copilot to use for sending the message.

Returns

- sendAiMessage(messageOrOptions: string \| SendAiMessageOptions) => AiChatMessage

A function that sends a message to an AI chat. Can be called with either a
string message or an options object containing `text`, optional `chatId`,
and optional `copilotId`.

#### [Setting options when sending a message](/content/docs/api-reference/liveblocks-react\#Setting-options-when-sending-a-message/index.html)

Optionally you can set options when sending a message, instead of when creating
the hook. Alternatively, you can also override or complete the hook’s options
when calling the function by passing an object to it.

```tsx
import { useSendAiMessage } from "@liveblocks/react";
// Setting a `chatId` and `copilotId`const sendAiMessage = useSendAiMessage("my-chat-id", {  copilotId: "co_shSm8f...",});
// Sends to initial `chatId` and `copilotId`sendAiMessage("Hello!");
// Overwrites the `copilotId` just for this messagesendAiMessage({  text: "Hello world",  copilotId: "co_Xpksa9...",});
// Overwrites the `chatId` just for this messagesendAiMessage({  text: "Hello world",  chatId: "my-other-chat-id",});
```

You can even skip setting the `chatId` and `copilotId` in the hook, and just
pass them in the function.

```tsx
const sendAiMessage = useSendAiMessage();
sendAiMessage({  text: "Hello world",  chatId: "my-other-chat-id",});
```

#### [Get the created message object](/content/docs/api-reference/liveblocks-react\#Get-the-created-message-object/index.html)

If necessary, you can also access the newly created message object.

```tsx
import { useSendAiMessage } from "@liveblocks/react";
const sendAiMessage = useSendAiMessage("my-chat-id");const message = sendAiMessage("Hello world");
//  { id: "ms_gw1wEn...", chatId: "my-chat-id", content: [...], ...}console.log(message);
```

### [useAiChatMessages](/content/docs/api-reference/liveblocks-react\#useAiChatMessages/index.html)

Returns a list of every message in an AI chat, identified by its ID. Updates in
realtime using WebSockets.

```tsx
import { useAiChatMessages } from "@liveblocks/react";
const { messages, error, isLoading } = useAiChatMessages("my-chat-id");
```

Arguments

- chatIdstring

The ID of the AI chat to retrieve messages from.

Returns

- messagesAiChatMessage\[\] \| undefined

An array of messages in the AI chat.

Example messages

- isLoadingboolean

Whether the messages are currently being loaded.

- errorError \| undefined

Any error that occurred while loading the messages.

### [useAiChatStatus](/content/docs/api-reference/liveblocks-react\#useAiChatStatus/index.html)

Returns the status of an AI chat, indicating whether it’s disconnected, loading,
idle or actively generating content. This is a convenience hook that derives its
state from the latest assistant message in the chat.

```tsx
import { useAiChatStatus } from "@liveblocks/react";
const { status, partType, toolName } = useAiChatStatus("my-chat-id");
```

Arguments

- chatIdstring

The ID of the AI chat.

Returns

- status"disconnected" \| "loading" \| "idle" \| "generating"

The current synchronization status of the chat. When disconnected the AI
service is not available.

- partType"text" \| "tool-invocation"

The type of content being generated.

- toolNamestring \| undefined

The name of the tool being invoked. If no tool is currently being called,
returns `undefined`.

### [RegisterAiKnowledge](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge/index.html)

Adds knowledge to all AI features on the page. AI will understand the
information you pass, and will answer questions or call tools based on it. This
is particularly helpful for passing user info, app state, and other small
contextual knowledge.

```tsx
<RegisterAiKnowledge  description="The current user's payment plan"  value="Enterprise"/>
```

Each knowledge source has a `description` string, and a `value` which can be
either a string, object, array or JSON-serializable value that provides
meaningful context for your use case. The AI uses this data, along with the
accompanying description, to better understand and respond to user queries or
perform actions based on the supplied context. These components can be placed
anywhere in your app, so long as they’re under
[`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html).

```tsx
import { AiChat } from "@liveblocks/react-ui";import { RegisterAiKnowledge } from "@liveblocks/react";
function Chat() {  return (    <>      <RegisterAiKnowledge        description="The current user's payment plan"        value="Enterprise"      />      <RegisterAiKnowledge        description="The current user's info"        value={{          name: "Jody Hekla",          email: "jody@liveblocks.io",          teams: ["Engineering", "Product"],        }}      />      <AiChat chatId="my-chat-id" />    </>  );}
```

#### [Pass in assorted context](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge-context/index.html)

Passing the AI context about the current datetime, the user’s language, the page
the user’s visiting, and navigable pages on your website, is an effective method
for improving your chat’s replies.

```tsx
import { RegisterAiKnowledge } from "@liveblocks/react";
function Chat() {  return (    <>      <RegisterAiKnowledge        description="The current date and time"        value={new Date()}      />      <RegisterAiKnowledge        description="The user's preferred language"        value={navigator.language}      />      <RegisterAiKnowledge        description="The current URL"        value={window.location.href}      />      <RegisterAiKnowledge        description="URLs on this website"        value={["/dashboard", "/billing", "/settings"]}      />    </>  );}
```

When building your app, it’s worth considering which app-specific context will
be helpful, for example the user’s payment plan, or a list of their projects.

#### [Pass in user data from your auth provider](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge-user/index.html)

You can pass in knowledge from your auth provider, for example with
[`useUser`](https://clerk.com/docs/hooks/use-user) from
[Clerk](https://clerk.com/). You can tell AI that the state is loading in a
simple string, and it will understand.

```tsx
import { RegisterAiKnowledge } from "@liveblocks/react";import { useUser } from "@clerk/clerk-react";
function Chat() {  const { isSignedIn, user, isLoaded } = useUser();
  return (    <RegisterAiKnowledge      description="The current user's info"      value={isLoaded ? (isSignedIn ? user : "Not signed in") : "Loading..."}    />  );}
```

#### [Pass in assorted data from fetching hooks](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge-fetching/index.html)

You can pass in knowledge from data fetching hooks such as with
[`useSWR`](https://swr.vercel.app/docs/getting-started) from
[SWR](https://swr.vercel.app/). You can tell AI that the state is loading in a
simple string, and it will understand.

```tsx
import { RegisterAiKnowledge } from "@liveblocks/react";import useSWR from "swr";
function Chat() {  const { data, error, isLoading } = useSWR(`/important-data`, fetcher);
  return (    <RegisterAiKnowledge      description="Important data"      value={isLoading ? "Loading..." : error ? "Problem fetching data" : data}    />  );}
const fetcher = (...args) => fetch(...args).then((res) => res.json());
```

#### [Pass in text editor document data](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge-text-editor/index.html)

You can pass in knowledge from your text editor, for example when using
[Liveblocks Tiptap](/content/docs/api-reference/liveblocks-react-tiptap/index.html).

```tsx
import { RegisterAiKnowledge } from "@liveblocks/react-ui";import { useLiveblocksExtension } from "@liveblocks/react-tiptap";import { useEditor, EditorContent } from "@tiptap/react";
function TextEditor() {  const liveblocks = useLiveblocksExtension();
  const editor = useEditor({    extensions: [      liveblocks,      // ...    ],  });
  return (    <div>      <RegisterAiKnowledge        description="The current document's Tiptap state"        value={editor.getHTML()}      />      <EditorContent editor={editor} />    </div>  );}
```

As well as `editor.getHTML()`, `editor.getJSON()` and `editor.getText()` are
also available when using Tiptap. Its worth trying them all, in case your AI
model understands one of them better than the others.

#### [Pass in comment data](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge-storage/index.html)

You can also pass in knowledge from your custom Liveblocks Comments app with
[`useThreads`](/content/docs/api-reference/liveblocks-react#useThreads/index.html). This way, your
AI chat will understand the context in the current room.

```tsx
import { RegisterAiKnowledge, useThreads } from "@liveblocks/react";
function Comments() {  const { threads, isLoading, error } = useThreads();
  return (    <div>      <RegisterAiKnowledge        description="The comment threads in the current document"        value={          isLoading            ? "Loading..."            : error              ? "Problem fetching threads"              : threads        }      />      {threads.map((thread) => (        <Thread key={thread.id} thread={thread} />      )}    </div>  );}
```

#### [Pass in storage data](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge-storage/index.html)

You can also pass in knowledge from your custom Liveblocks storage app with
[`useStorage`](/content/docs/api-reference/liveblocks-react#useStorage/index.html). This way, your
AI chat will understand the context in the current room.

```tsx
import { RegisterAiKnowledge, useStorage } from "@liveblocks/react/suspense";
function Whiteboard() {  const shapes = useStorage((root) => root.shapes);
  return (    <div>      <RegisterAiKnowledge        description="The current shapes on the whiteboard"        value={shapes}      />      {shapes.map((shape) => (        // ...      )}    </div>  );}
```

#### [Props](/content/docs/api-reference/liveblocks-react\#RegisterAiKnowledge-props/index.html)

- descriptionstringRequired

A clear description of what this knowledge represents. This helps the AI
understand the context and relevance of the provided information.

- valueJsonRequired

The actual data or information to share with the AI. Can be a string,
object, array, or any JSON-serializable information that offers context
relevant to your application or user.

- idstring

Optional unique identifier for this knowledge source. If provided,
subsequent updates with the same ID will replace the previous knowledge.

- chatIdstring

Optional chat ID to scope this tool to a specific chat. If provided, the
tool will only be available to that chat.

### [RegisterAiTool](/content/docs/api-reference/liveblocks-react\#RegisterAiTool/index.html)

Registers a tool that can be used by
[AI chats](/content/docs/api-reference/liveblocks-react-ui#AiChat/index.html) on the page. Tools
allow AI to autonomously run actions, render custom components, and show
confirmation or human-in-the-loop UIs within the chat.

```tsx
import { RegisterAiTool } from "@liveblocks/react";import { defineAiTool } from "@liveblocks/client";
<RegisterAiTool  name="my-tool"  tool={defineAiTool()({    // Tool definition    // ...  })}/>;
```

[`defineAiTool`](/content/docs/api-reference/liveblocks-client#defineAiTool/index.html) is used to
create a tool definition, and you can supply `parameters` as a
[JSON Schema](https://json-schema.org/) that the AI can fill in. If you supply
an `execute` function the AI will call it. `render` is used to show UI inside
the chat. Below is an example of a tool that lets AI get the current weather in
a given location, then renders a component in the chat.

```tsx
function App() {  return (    <>      <RegisterAiTool        name="get-weather"        tool={defineAiTool()({          description: "Get current weather information",          parameters: {            type: "object",            properties: {              location: { type: "string", description: "City name" },            },            required: ["location"],            additionalProperties: false,          },          execute: async (args) => {            const { temperature, condition } = await __getWeather__(              args.location            );            return { data: { temperature, condition } };          },          render: ({ result }) => (            <AiTool title="Weather Lookup" icon="🌤️">              {result.data ? (                <div>                  {result.data.temperature}°F - {result.data.condition}                </div>              ) : null}            </AiTool>          ),        })}      />      <AiChat chatId="my-chat" />    </>  );}
```

#### [Tool that sends a toast notification](/content/docs/api-reference/liveblocks-react\#Tool-that-sends-a-toast-notification/index.html)

The following snippet shows a tool that lets AI send a toast notification with
[Sonner](https://sonner.emilkowal.ski/), then adds a message in the chat with
[`AiTool`](/content/docs/api-reference/liveblocks-react-ui#AiTool/index.html), letting the user
know that a toast was sent.

```tsx
import { AiChat } from "@liveblocks/react-ui";import { RegisterAiTool } from "@liveblocks/react";import { defineAiTool } from "@liveblocks/client";import { toast, Toaster } from "sonner";
function Chat() {  return (    <>      <RegisterAiTool        name="send-toast-notification"        tool={defineAiTool()({          description: "Send a toast notification",
          parameters: {            type: "object",            properties: {              message: {                type: "string",                description: "The message to display in the toast",              },            },            required: ["message"],            additionalProperties: false,          },
          execute: async ({ message }) => {            toast(message);            return {              data: { message },              description: "You sent a toast",            };          },
          render: () => <AiTool title="Toast sent" icon="🍞" />,        })}      />      <AiChat chatId="my-chat-id" />      <Toaster />    </>  );}
```

#### [Scoping a tool to a specific chat](/content/docs/api-reference/liveblocks-react\#Scoping-a-tool-to-a-specific-chat/index.html)

Tools can be scoped to specific chats by providing a `chatId` prop. When scoped,
the tool will only be available to that specific chat.

```tsx
import { AiChat } from "@liveblocks/react-ui";import { RegisterAiTool } from "@liveblocks/react";import { defineAiTool } from "@liveblocks/client";
function Chat() {  return (    <>      <RegisterAiTool        name="private-tool"        tool={defineAiTool()({          // Tool definition          // ...        })}        chatId="my-chat"      />      <AiChat chatId="my-chat" />    </>  );}
```

#### [Props](/content/docs/api-reference/liveblocks-react\#RegisterAiTool-props/index.html)

- namestringRequired

Unique name for the tool. This is used internally to identify and manage the
tool.

- toolAiOpaqueToolDefinitionRequired

The tool definition created with
[`defineAiTool`](/content/docs/api-reference/liveblocks-client#defineAiTool/index.html).

- chatIdstring

Optional chat ID to scope this tool to a specific chat. If provided, the
tool will only be available to that chat.

- enabledboolean

Whether this tool should be enabled. When set to `false`, the tool will not
be made available to the AI copilot for any new/future chat messages, but
will still allow existing tool invocations to be rendered that are part of
the historic chat record. When provided as a prop to `RegisterAiTool`, it
will take precedence over the value of the tool’s `enabled` value in
`defineAiTool`.

## [Room](/content/docs/api-reference/liveblocks-react\#Room/index.html)

### [RoomProvider](/content/docs/api-reference/liveblocks-react\#RoomProvider/index.html)

Makes a [`Room`](/content/docs/api-reference/liveblocks-client#Room/index.html) available in the component hierarchy below. Joins the room
when the component is mounted, and automatically leaves the room when the
component is unmounted. When using
[Sync Datastore](/content/docs/platform/sync-datastore/index.html), initial Presence values for
each user, and Storage values for the room can be set.

```tsx
import { RoomProvider } from "@liveblocks/react/suspense";
function App() {  return <RoomProvider id="my-room-id">{/* children */}</RoomProvider>;}
```

Props

- idstringRequired

The unique ID for the current room. `RoomProvider` will join this room when
it loads. If the room doesn’t exist already it will automatically create the
room first then join. After setting up
[authentication](/content/docs/authentication/index.html) for your app, it can helpful to
decide on a naming pattern for your room IDs.

- initialPresenceJsonObject

The initial Presence of the user entering the room. Each user has their own
presence, and this is readable for all other connected users. A user’s
Presence resets every time they disconnect. This object must be
JSON-serializable. This value is ignored after the first render. [Learn\\
more](/content/docs/api-reference/liveblocks-react#setting-initial-presence/index.html).

- initialStorageLsonObject

The initial Storage structure for the room when it’s joined for the first
time. This is only set a single time, when the room has not yet been
populated. This object must contain [conflict-free live\\
structures](/content/docs/api-reference/liveblocks-client#Storage/index.html). This value is
ignored after the first render, and if Storage for the current room has
already been created. [Learn more](/content/docs/api-reference/liveblocks-react#setting-initial-storage/index.html).

- autoConnectbooleanDefault istrue

Whether the room immediately connects to Liveblocks servers. This value is
ignored after the first render.

- engine1 \| 2

Preferred storage engine version to use when creating the room. Only takes
effect if the room doesn’t exist yet. The v2 Storage engine supports larger
documents, is more performant, has native streaming support, and will become
the default in the future. [Learn\\
more](/content/docs/guides/about-the-new-storage-engine/index.html).

#### [Setting initial Presence](/content/docs/api-reference/liveblocks-react\#setting-initial-presence/index.html)

Presence is used for storing temporary user-based values, such as a user’s
cursor coordinates, or their current selection. Each user has their own
presence, and this is readable for all other connected users. Set your initial
Presence value by using `initialPresence`.

```tsx
import { RoomProvider } from "@liveblocks/react/suspense";
function App() {  return (    <RoomProvider      id="my-room"      initialPresence={{        cursor: null,        colors: ["red", "purple"],        selection: {          id: 72426,        },      }}    >      {/* children */}    </RoomProvider>  );}
```

Each user’s Presence resets every time they disconnect, as this is only meant
for temporary data. Any JSON-serializable object is allowed (the `JsonObject`
type).

#### [Setting initial Storage](/content/docs/api-reference/liveblocks-react\#setting-initial-storage/index.html)

Storage is used to store permanent data that’s used in your application, such as
shapes on a whiteboard, nodes on a flowchart, or text in a form. The first time
a room is entered, you can set an initial value by using `initialStorage`.
`initialStorage` is only read and set a single time, unless a new top-level
property is added.

```tsx
import { LiveList, LiveObject, LiveMap } from "@liveblocks/client";import { RoomProvider } from "@liveblocks/react/suspense";
function App() {  return (    <RoomProvider      id="my-room"      initialPresence={}      initialStorage={{        title: "Untitled",        names: new LiveList(["Steven", "Guillaume"]),        shapes: new LiveMap([          ["g9shu0", new LiveObject({ type: "rectangle", color: "red" })],          ["djs3g5", new LiveObject({ type: "circle", color: "yellow" })],        ]),      }}    >      {/* children */}    </RoomProvider>  );}
```

If a new top-level property is added to `initialStorage`, the next time a user
connects, the new property will be created. Other properties will be unaffected.
Any
[conflict-free live structures](/content/docs/api-reference/liveblocks-client#Storage/index.html)
and JSON-serializable objects are allowed (the `LsonObject` type).

#### [Speed up connecting to a room](/content/docs/api-reference/liveblocks-react\#speed-up-connecting-to-a-room/index.html)

To speed up connecting to a room, you can call
[`Liveblocks.prewarmRoom`](/content/docs/api-reference/liveblocks-node#get-rooms-roomId-prewarm/index.html)
on the server, which will warm up a room for the next 10 seconds. Triggering
this directly before a user navigates to a room is an easy to way use this API.
Here’s a Next.js server actions example, showing how to trigger prewarming with
`onPointerDown`.

actions.ts

[Sign in](/content/dashboard/apikeys/index.html) to get your own API keys.

```ts
"use server";
import { Liveblocks } from "@liveblocks/node";
const liveblocks = new Liveblocks({  secret: "sk_prod_xxxxxxxxxx…xxxxxxxxsk_prod_xxxxxxxxxxxxxxxxxxxxxxxx",});
export async function prewarmRoom(roomId: string) {  await liveblocks.prewarmRoom(roomId);}
```

RoomLink.tsx

```tsx
"use client";
import { prewarmRoom } from "../actions";import Link from "next/link";
export function JoinButton({ roomId }: { roomId: string }) {  return (    <Link href={`/rooms/${roomId}`} onPointerDown={() => prewarmRoom(roomId)}>      {roomId}    </Link>  );}
```

`onPointerDown` is slightly quicker than `onClick` because it triggers before
the user releases their pointer.

### [createRoomContext](/content/docs/api-reference/liveblocks-react\#createRoomContext/index.html)

This used to be the default way to start your app, but now it’s recommend for
advanced usage only. We generally recommend using [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html) and
following [typing your data with the Liveblocks interface](/content/docs/api-reference/liveblocks-react#Typing-your-data/index.html),
unless you need to define multiple room types in your application.

Creates a [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html) and a set of typed hooks to use in your app. Note
that any `RoomProvider` created in this way does not need to be nested in
[`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html), as it already has access to the `client`. We generally
recommend typing your app using the newer method instead. When using
`createRoomContext` it can be helpful to use it in `liveblocks.config.ts` and
re-export your typed hooks as below.

liveblocks.config.ts

```tsx
import { createClient } from "@liveblocks/client";import { createRoomContext } from "@liveblocks/react";
const client = createClient({  // publicApiKey: "",  // authEndpoint: "/api/liveblocks-auth",});
type Presence = {};type Storage = {};type UserMeta = {};type RoomEvent = {};type ThreadMetadata = {};type CommentMetadata = {};
export const {  RoomProvider,  useMyPresence,
  // Other hooks  // ...} = createRoomContext<  Presence,  Storage,  UserMeta,  RoomEvent,  ThreadMetadata,  CommentMetadata>(client);
```

#### [Suspense with createRoomContext](/content/docs/api-reference/liveblocks-react\#createRoomContext-Suspense/index.html)

To use the React suspense version of our hooks with `createRoomContext`, you can
export from the `suspense` property instead.

liveblocks.config.ts

```tsx
import { createClient } from "@liveblocks/client";import { createRoomContext } from "@liveblocks/react";
const client = createClient({  // publicApiKey: "",  // authEndpoint: "/api/liveblocks-auth",});
type Presence = {};type Storage = {};type UserMeta = {};type RoomEvent = {};type ThreadMetadata = {};type CommentMetadata = {};
export const {  suspense: {    RoomProvider,    useMyPresence,
    // Other suspense hooks    // ...  },} = createRoomContext<  Presence,  Storage,  UserMeta,  RoomEvent,  ThreadMetadata,  CommentMetadata>(client);
```

#### [Typing createRoomContext](/content/docs/api-reference/liveblocks-react\#Typing-createRoomContext/index.html)

To type your hooks, you can pass multiple different types to
`createRoomContext`. A full explanation is in the code snippet below.

liveblocks.config.ts

### [useRoomRoomProvider](/content/docs/api-reference/liveblocks-react\#useRoom/index.html)

Returns the [`Room`](/content/docs/api-reference/liveblocks-client#Room/index.html) of the nearest [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html) above in the React
component tree.

```ts
import { useRoom } from "@liveblocks/react/suspense";
const room = useRoom();
```

Will throw when used outside of a [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html). If you don’t want this
hook to throw when used outside of a Room context (for example to write
components in a way that they can be used both inside and outside of a
Liveblocks room), you can use the `{ allowOutsideRoom }` option:

```ts
import { useRoom } from "@liveblocks/react/suspense";
const room = useRoom({ allowOutsideRoom: true }); // Possibly `null`
```

Options

- allowOutsideRoombooleanDefault isfalse

Whether the hook should return `null` instead of throwing when used outside
of a [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html) context.

Returns

- roomRoom

The Room instance from the nearest [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html). Returns `null` if
`allowOutsideRoom` is `true` and the hook is used outside of a room.

### [useIsInsideRoomBoth](/content/docs/api-reference/liveblocks-react\#useIsInsideRoom/index.html)

Returns a boolean, `true` if the hook was called inside a [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html)
context, and `false` otherwise.

```ts
import { useIsInsideRoom } from "@liveblocks/react/suspense";
const isInsideRoom = useIsInsideRoom();
```

Arguments

_None_

Returns

- isInsideRoomboolean

`true` if the hook was called inside a [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html) context, `false`
otherwise.

#### [Displaying different components inside rooms](/content/docs/api-reference/liveblocks-react\#Displaying-different-components-inside-rooms/index.html)

`useIsInsideRoom` is helpful for rendering different components depending on
whether they’re inside a room, or not. One example is a header component that
only displays a live avatar stack when users are connected to the room.

```tsx
import { useIsInsideRoom, useOthers } from "@liveblocks/react/suspense";
function Header() {  const isInsideRoom = useIsInsideRoom();
  return (    <div>      {isInsideRoom ? <LiveAvatars /> : null}      <MyAvatar />    </div>  );}
function LiveAvatars() {  const others = useOthers();  return others.map((other) => <img src={other.info.picture} />);}
```

Here’s how the example above would render in three different
[`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html)
and [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html)
contexts.

```tsx
// 👥👤 Live avatar stack and your avatar<LiveblocksProvider /* ... */>  <RoomProvider /* ... */>    <Header />  </RoomProvider></LiveblocksProvider>

// 👤 Just your avatar<LiveblocksProvider /* ... */>  <Header /></LiveblocksProvider>
// 👤 Just your avatar<Header />
```

### [useStatusRoomProvider](/content/docs/api-reference/liveblocks-react\#useStatus/index.html)

Returns the current WebSocket connection status of the room, and will re-render
your component whenever it changes.

```ts
import { useStatus } from "@liveblocks/react/suspense";
const status = useStatus();
```

Arguments

_None_

Returns

- status'initial' \| 'connecting' \| 'connected' \| 'reconnecting' \| 'disconnected'

The current WebSocket connection status of the room.

### [useSyncStatusBoth](/content/docs/api-reference/liveblocks-react\#useSyncStatus/index.html)

Returns the current synchronization status of Liveblocks, and will re-render
your component whenever it changes. This includes any part of Liveblocks that
may be synchronizing local changes to the server, including (any room’s)
Storage, text editors, threads, or notifications.

A `{ smooth: true }` option is also available, which prevents quick changes
between states, making it ideal for
[rendering a synchronization badge in your app](/content/docs/api-reference/liveblocks-react#display-synchronization-badge/index.html).
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```ts
import { useSyncStatus } from "@liveblocks/react/suspense";
// "synchronizing" | "synchronized"const syncStatus = useSyncStatus();
```

Arguments

- optionsobject

Optional configuration object.

- options.smoothbooleanDefault isfalse

When `true`, prevents quick changes between states by delaying the
transition from "synchronizing" to "synchronized" until 1 second has passed
after the final change.

Returns

- syncStatus'synchronizing' \| 'synchronized'

The current synchronization status of Liveblocks.

#### [Display a synchronization badge](/content/docs/api-reference/liveblocks-react\#display-synchronization-badge/index.html)

Passing `{ smooth: true }` prevents the status changing from `"synchronizing"`
to `"synchronized"` until 1 second has passed after the final change. This means
it’s ideal for rendering a synchronization status badge, as it won’t flicker in
a distracting manner when changes are made in quick succession.

```tsx
import { useSyncStatus } from "@liveblocks/react/suspense";
function StorageStatusBadge() {  const syncStatus = useSyncStatus({ smooth: true });
  return <div>{syncStatus === "synchronized" ? "✅ Saved" : "🔄 Saving"}</div>;}
```

#### [Prevent users losing unsaved changes](/content/docs/api-reference/liveblocks-react\#use-sync-status-prevent-users-losing-unsaved-changes/index.html)

```tsx
function Page() {  return (    <LiveblocksProvider      preventUnsavedChanges
      // Other options      // ...    >      ...    </LiveblocksProvider>  );}
```

More specifically, this option triggers when:

Internally, this option uses the
[beforeunload event](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event).

### [useOthersListenerRoomProvider](/content/docs/api-reference/liveblocks-react\#useOthersListener/index.html)

Calls the given callback when an “others” event occurs, when a user enters,
leaves, or updates their presence.

```ts
function App() {  useOthersListener(({ type, user, others }) => {    switch (type) {      case "enter":        // `user` has entered the room        break;
      case "leave":        // `user` has left the room        break;
      case "update":        // Presence for `user` has updated        break;
      case "reset":        // Others list has been emptied        break;    }  });}
```

Arguments

- callback(event: OthersEvent) => void

A callback function that is called when an "others" event occurs. The event
object contains the error `type`, the `user` that triggered it, and the current
`others` in the room. Possible event types are:

- `enter` – A user has entered the room.
  - `leave` – A user has left the room.
  - `reset` – The others list has been emptied. This is the first event that
    occurs when the room is entered. It also occurs when you’ve lost connection to
    the room.
  - `update` – A user’s presence data has been updated.

### [useLostConnectionListenerRoomProvider](/content/docs/api-reference/liveblocks-react\#useLostConnectionListener/index.html)

Calls the given callback in the exceptional situation that a connection is lost
and reconnecting does not happen quickly enough.

This event allows you to build high-quality UIs by warning your users that the
app is still trying to re-establish the connection, for example through a toast
notification. You may want to take extra care in the mean time to ensure their
changes won’t go unsaved.

When this happens, this callback is called with the event `lost`. Then, once the
connection restores, the callback will be called with the value `restored`. If
the connection could definitively not be restored, it will be called with
`failed` (uncommon).

The [`lostConnectionTimeout`](/content/docs/api-reference/liveblocks-client#createClientLostConnectionTimeout/index.html) client option will determine how quickly this
event will fire after a connection loss (default: 5 seconds).

```ts
import { toast } from "my-preferred-toast-library";
function App() {  useLostConnectionListener((event) => {    switch (event) {      case "lost":        toast.warn("Still trying to reconnect...");        break;
      case "restored":        toast.success("Successfully reconnected again!");        break;
      case "failed":        toast.error("Could not restore the connection");        break;    }  });}
```

Automatically unsubscribes when the component is unmounted. For a demonstration
of this behavior, see our [connection status example](/content/examples/connection-status/nextjs/index.html).

Arguments

- callback(event: 'lost' \| 'restored' \| 'failed') => void

A callback function that is called when a connection loss event occurs. The
event can be "lost", "restored", or "failed".

## [Presence](/content/docs/api-reference/liveblocks-react\#Presence/index.html)

Need help troubleshooting presence?

Try the [Liveblocks DevTools extension](/content/devtools/index.html) to visualize your
collaborative experiences as you build them, in realtime.

### [useMyPresenceRoomProvider](/content/docs/api-reference/liveblocks-react\#useMyPresence/index.html)

Return the presence of the current user, and a function to update it.
Automatically subscribes to updates to the current user’s presence. Setting a
property will not replace the whole state, but will instead merge the property
into the existing state.

```ts
import { useMyPresence } from "@liveblocks/react/suspense";
const [myPresence, updateMyPresence] = useMyPresence();
updateMyPresence({ x: 0 });updateMyPresence({ y: 0 });
// At the next render, "myPresence" will be equal to "{ x: 0, y: 0 }"
```

Returns

- myPresenceTPresence

The current user’s presence data.

- updateMyPresence(patch: Partial<Presence>, options?: { addToHistory?: boolean }) => void

A function to update the current user’s presence. Accepts a partial presence
object and optional history options.

#### [Adding presence to history](/content/docs/api-reference/liveblocks-react\#Adding-presence-to-history/index.html)

`updateMyPresence` accepts an optional argument to add a new item to the
undo/redo stack. See [`room.history`](/content/docs/api-reference/liveblocks-client#Room.history) for more information.

```ts
updateMyPresence({ selectedId: "xxx" }, { addToHistory: true });
```

#### [Other ways to use presence](/content/docs/api-reference/liveblocks-react\#Other-ways-to-use-presence/index.html)

`useMyPresence` is a more convenient way to update and view presence, rather
than using [`useSelf`](/content/docs/api-reference/liveblocks-react#useSelf/index.html) and [`useUpdateMyPresence`](/content/docs/api-reference/liveblocks-react#useUpdateMyPresence/index.html) in combination.

```tsx
const myPresence = useSelf((me) => me.presence);const updateMyPresence = useUpdateMyPresence();
```

### [useUpdateMyPresenceRoomProvider](/content/docs/api-reference/liveblocks-react\#useUpdateMyPresence/index.html)

Returns a setter function to update the current user’s presence. Setting a
property will not replace the whole state, but will instead merge the property
into the existing state. Will trigger fewer renders than [`useMyPresence`](/content/docs/api-reference/liveblocks-react#useMyPresence/index.html), as
it doesn’t update when presence changes.

```ts
import { useUpdateMyPresence } from "@liveblocks/react/suspense";
const updateMyPresence = useUpdateMyPresence();
updateMyPresence({ y: 0 });updateMyPresence({ x: 0 });
// Presence will be { x: 0, y: 0 }
```

Arguments

_None_

Returns

- updateMyPresence(patch: Partial<Presence>, options?: { addToHistory?: boolean }) => void

A function to update the current user's presence. Accepts a partial presence
object and optional history options.

#### [Adding presence to history](/content/docs/api-reference/liveblocks-react\#Adding-presence-to-history/index.html)

```ts
updateMyPresence({ selectedId: "xxx" }, { addToHistory: true });
```

### [useSelfRoomProvider](/content/docs/api-reference/liveblocks-react\#useSelf/index.html)

Returns the current user once it is connected to the room, and automatically
subscribes to updates to the current user.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```ts
import { useSelf } from "@liveblocks/react/suspense";
const currentUser = useSelf();// {//   connectionId: 1,//   presence: { cursor: { x: 27, y: -8 } },// }
const currentUser = useSelf((me) => me.presence.cursor);// { x: 27, y: -8 }
```

The benefit of using a selector is that it will only update your component if
that particular selection changes. For full details, see [how selectors work](/content/docs/api-reference/liveblocks-react#selectors/index.html).

Arguments

- selector(me: User) => T

Optional selector function to extract specific data from the current user.
If not provided, returns the entire user object.

Returns

- currentUserUser \| T \| null

The current user object or the selected data from the user. Returns `null`
if not connected to the room (in non-Suspense version).

#### [Checking user permissions](/content/docs/api-reference/liveblocks-react\#Checking-user-permissions/index.html)

It’s possible to check if a user has a specific permission by using the
`canWrite` and `canComment` properties of the `User` object. This is set via
your [room permissions](/content/docs/authentication#Room-permissions/index.html).

```ts
import { useSelf } from "@liveblocks/react/suspense";
function PermissionBadge() {  const canWrite = useSelf((me) => me.canWrite);
  if (canWrite) {    return <div>✏️ Full access</div>;  }
  return <div>👀 Read-only access</div>;}
```

This is particularly helpful in combination with text editors, such as
[Tiptap](/content/docs/api-reference/liveblocks-react-tiptap/index.html) as you can prevent
read-only users from editing the document.

```tsx
import { useSelf } from "@liveblocks/react/suspense";import { useLiveblocksExtension } from "@liveblocks/react-tiptap";import { useEditor, EditorContent } from "@tiptap/react";
function TextEditor() {  const liveblocks = useLiveblocksExtension();  const canWrite = useSelf((me) => me.canWrite);
  const editor = useEditor({    editable: canWrite,    extensions: [      liveblocks,      // ...    ],  });
  return (    <div>      <EditorContent editor={editor} />    </div>  );}
```

### [useOthersRoomProvider](/content/docs/api-reference/liveblocks-react\#useOthers/index.html)

Extracts data from the list of other users currently in the same Room, and
automatically subscribes to updates on the selected data. For full details, see
[how selectors work](/content/docs/api-reference/liveblocks-react#selectors/index.html).
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

The `others` argument to the `useOthers` selector function is an _immutable_
array of Users.

```tsx
// ✅ Rerenders only if the number of users changesconst numOthers = useOthers((others) => others.length);
// ✅ Rerenders only if someone starts or stops typingconst isSomeoneTyping = useOthers((others) =>  others.some((other) => other.presence.isTyping));
// ✅ Rerenders only if actively typing users are updatedconst typingUsers = useOthers(  (others) => others.filter((other) => other.presence.isTyping),  shallow // 👈);
```

One caveat with this API is that selecting a subset of data for each user
quickly becomes tricky. When you want to select and get updates for only a
particular subset of each user’s data, we recommend using the
[`useOthersMapped`](/content/docs/api-reference/liveblocks-react#useOthersMapped/index.html) hook instead, which is optimized for this use case.

```tsx
// ❌ Mapping is hard to get right with this hookconst cursors = useOthers(  (others) => others.map((other) => other.presence.cursor),  shallow);
// ✅ Better to use useOthersMappedconst cursors = useOthersMapped((other) => other.presence.cursor);
```

When called without arguments, returns the user list and updates your component
whenever _anything_ in it changes. This might be way more often than you want!

```tsx
const others = useOthers(); // ⚠️ Caution, might rerender often!// [//   { connectionId: 2, presence: { cursor: { x: 27, y: -8 } } },//   { connectionId: 3, presence: { cursor: { x: 0, y: 19 } } },// ]
```

Caution

In production-ready apps, you likely want to avoid calling `useOthers` without
arguments.

Arguments

- selector(others: readonly User\[\]) => T

Optional selector function to extract specific data from the others array.
If not provided, returns the entire others array.

- isEqual(prev: T, curr: T) => boolean

Optional equality function to determine if the selected data has changed.
Defaults to strict equality comparison.

Returns

- othersreadonly TUser\[\] \| null

The others array or the selected data from the others array. Returns `null`
if not connected to the room (in non-Suspense version).

### [useOthersMappedRoomProvider](/content/docs/api-reference/liveblocks-react\#useOthersMapped/index.html)

Extract data using a [selector](/content/docs/api-reference/liveblocks-react#selectors/index.html) for every user in the room, and subscribe to
all changes to the selected data. A [Suspense version](/content/docs/api-reference/liveblocks-react#Suspense/index.html) of this hook is also
available. The key difference with [`useOthers`](/content/docs/api-reference/liveblocks-react#useOthers/index.html) is that the selector (and the
optional comparison function) work at the _item_ level, like doing a `.map()`
over the others array.

```tsx
// Example 1const others = useOthersMapped((other) => other.presence.cursor);// [//   [2, { x: 27, y: -8 }],//   [3, { x: 0, y: 19 }],// ]
// Example 2const others = useOthersMapped(  (other) => ({    avatar: other.info.avatar,    isTyping: other.presence.isTyping,  }),  shallow // 👈);
// [//   [2, { avatar: 'https://...', isTyping: true }],//   [3, { avatar: null, isTyping: false }],// ]
```

Returns an array where each item is a pair of `[connectionId, data]`. For
pragmatic reasons, the results are keyed by the `connectionId`, because in most
cases you’ll want to iterate over the results and draw some UI for each, which
in React requires you to use a `key={connectionId}` prop.

```tsx
const others = useOthersMapped((other) => other.presence.cursor);
// In JSXreturn (  <>    {others.map(([connectionId, cursor]) => (      <Cursor key={connectionId} x={cursor.x} y={cursor.y} />    ))}  </>);
```

Arguments

- selector(other: User) => T

A selector function to extract specific data from each user in the others
array.

- isEqual(prev: T, curr: T) => boolean

Optional equality function to determine if the selected data for a user has
changed. Defaults to strict equality comparison.

Returns

- othersreadonly \[connectionId: number, data: T\]\[\] \| null

An array of tuples where each item is a pair of `[connectionId,     selectedData]`. Returns `null` if not connected to the room (in non-Suspense
version).

### [useOthersConnectionIdsRoomProvider](/content/docs/api-reference/liveblocks-react\#useOthersConnectionIds/index.html)

Returns an array of connection IDs (numbers), and rerenders automatically when
users join or leave. This hook is useful in particular in combination with the
[`useOther`](/content/docs/api-reference/liveblocks-react#useOther/index.html) (singular) hook, to implement high-frequency rerendering of
components for each user in the room, e.g. cursors. See the [`useOther`](/content/docs/api-reference/liveblocks-react#useOther/index.html)
(singular) documentation below for a full usage example.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useOthersConnectionIds } from "@liveblocks/react/suspense";
// [2, 4, 7]const connectionIds = useOthersConnectionIds();
```

Returns

- connectionIdsreadonly number\[\] \| null

An array of connection IDs for all other users in the room. Returns `null`
if not connected to the room (in non-Suspense version).

#### [Another way to fetch connection IDs](/content/docs/api-reference/liveblocks-react\#Another-way-to-fetch-connection-IDs/index.html)

This hook is similar to using [`useOthers`](/content/docs/api-reference/liveblocks-react#useOthers/index.html) and calling `.map()` on the
result.

```tsx
import { useOthers, shallow } from "@liveblocks/react/suspense";
// [2, 4, 7]const connectionIds = useOthers(  (others) => others.map((other) => other.connectionId),  shallow);
```

### [useOtherRoomProvider](/content/docs/api-reference/liveblocks-react\#useOther/index.html)

Extract data using a [selector](/content/docs/api-reference/liveblocks-react#selectors/index.html) for one specific user in the room, and
subscribe to all changes to the selected data.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useOther } from "@liveblocks/react/suspense";
// ✅ Rerenders when this specific user’s isTyping changes (but not when their cursor changes)const isTyping = useOther(  3, // User with connectionId 3  (user) => user.presence.isTyping);
```

The reason this hook exists is to enable the most efficient rerendering model
for high-frequency updates to other’s presences, which is the following
structure:

Cursors.tsx

```tsx
const Cursors =  // (1) Wrap parent component in a memo and make sure it takes no props  React.memo(function () {    const othersConnectionIds = useOthersConnectionIds(); // (2)    return (      <>        {othersConnectionIds.map((connectionId) => (          <Cursor            key={connectionId} // (3)            connectionId={connectionId}          />        ))}      </>    );  });
```

Cursor.tsx

```tsx
function Cursor({ connectionId }) {  const { x, y } = useOther(connectionId, (other) => other.presence.cursor); // (4)  return <Cursor x={x} y={y} />;}
```

1. Makes sure this whole component tree will never rerender beyond the first
time.
2. Makes sure the parent component only rerenders when users join/leave.
3. Makes sure each cursor remains associated to the same connection.
4. Makes sure each cursor rerenders whenever _its_ data changes only.

👉 A [Suspense version](/content/docs/api-reference/liveblocks-react#Suspense/index.html) of this hook is also available, which will never
return `null`.

Arguments

- connectionIdnumber

The connection ID of the specific user to extract data from.

- selector(other: User) => T

A selector function to extract specific data from the user.

- isEqual(prev: T, curr: T) => boolean

Optional equality function to determine if the selected data has changed.
Defaults to strict equality comparison.

Returns

- dataT \| null

The selected data from the specified user. Returns `null` if the user is not
found or not connected to the room (in non-Suspense version).

## [Broadcast](/content/docs/api-reference/liveblocks-react\#Broadcast/index.html)

### [useBroadcastEventRoomProvider](/content/docs/api-reference/liveblocks-react\#useBroadcastEvent/index.html)

Returns a callback that lets you broadcast custom events to other users in the
room.

```ts
import { useBroadcastEvent } from "@liveblocks/react/suspense";
// On client Aconst broadcast = useBroadcastEvent();broadcast({ type: "EMOJI", emoji: "🔥" });
// On client BuseEventListener(({ event, user, connectionId }) => {  //                       ^^^^ Will be Client A  if (event.type === "EMOJI") {    // Do something  }});
```

Arguments

_None_

Returns

- broadcast(event: TBroadcastEvent) => void

A function that broadcasts custom events to other users in the room.

### [useEventListenerRoomProvider](/content/docs/api-reference/liveblocks-react\#useEventListener/index.html)

Listen to custom events sent by other people in the room via
[`useBroadcastEvent`](/content/docs/api-reference/liveblocks-react#useBroadcastEvent/index.html). Provides the `event` along with the `connectionId` of
the user that sent the message. If an event was sent from the
[Broadcast to a room](/content/docs/api-reference/rest-api-endpoints#post-broadcast-event/index.html)
REST API, `connectionId` will be `-1`.

```ts
import { useEventListener } from "@liveblocks/react/suspense";
// On client Aconst broadcast = useBroadcastEvent();broadcast({ type: "EMOJI", emoji: "🔥" });
// On client BuseEventListener(({ event, user, connectionId }) => {  //                       ^^^^ Will be Client A  if (event.type === "EMOJI") {    // Do something  }});
```

The `user` property will indicate which User instance sent the message. This
will typically be equal to one of the others in the room, but it can also be
`null` in case this event was broadcasted from the server, using the
[Broadcast Event API](/content/docs/api-reference/rest-api-endpoints#post-broadcast-event/index.html).

Automatically unsubscribes when the component is unmounted.

Arguments

- callback(event: { event: TBroadcastEvent; user: User \| null; connectionId: number }) => void

A callback function that is called when a custom event is received. The
callback receives an object with the event data, user information, and
connection ID. Connection ID is always `-1` when receiving an event sent
from the server.

## [Storage](/content/docs/api-reference/liveblocks-react\#Storage/index.html)

Each room contains Storage, a conflict-free data store that multiple users can
edit at the same time. When users make edits simultaneously, conflicts are
resolved automatically, and each user will see the same state. Storage is ideal
for storing permanent document state, such as shapes on a canvas, notes on a
whiteboard, or cells in a spreadsheet.

### [Data structures](/content/docs/api-reference/liveblocks-react\#Data-structures/index.html)

Storage provides three different conflict-free data structures, which you can
use to build your application. All structures are permanent and persist when all
users have left the room, unlike [Presence](/content/docs/ready-made-features/presence/index.html)
which is temporary.

- [`LiveObject`](/content/docs/api-reference/liveblocks-client#LiveObject/index.html) \- Similar to JavaScript object. Use this for storing records
with fixed key names and where the values don’t necessarily have the same
types. For example, a `Person` with a `name: string` and an `age: number`
field. If multiple clients update the same property simultaneously, the last
modification received by the Liveblocks servers is the winner.

- [`LiveList`](/content/docs/api-reference/liveblocks-client#LiveList/index.html) \- An ordered collection of items synchronized across clients.
Even if multiple users add/remove/move elements simultaneously, LiveList will
solve the conflicts to ensure everyone sees the same collection of items.

- [`LiveMap`](/content/docs/api-reference/liveblocks-client#LiveMap/index.html) \- Similar to a JavaScript Map. Use this for indexing values that
all have the same structure. For example, to store an index of `Person` values
by their name. If multiple users update the same property simultaneously, the
last modification received by the Liveblocks servers is the winner.

### [Typing Storage](/content/docs/api-reference/liveblocks-react\#typing-storage/index.html)

To type the Storage values you receive, make sure to set your `Storage` type.

```ts
import { LiveList } from "@liveblocks/client";
declare global {  interface Liveblocks {    Storage: {      animals: LiveList<string>;    };  }}
```

You can then set an initial value in [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html).

```tsx
import { LiveList } from "@liveblocks/client";import { RoomProvider } from "@liveblocks/react/suspense";
function App() {  return (    <RoomProvider      id="my-room-name"      initialStorage={{ animals: new LiveList(["Fido"]) }}    >      {/* children */}    </RoomProvider>  );}
```

The type received in your Storage will match the type passed. Learn more under
[typing your data](/content/docs/api-reference/liveblocks-react#typing-your-data/index.html).

```tsx
import { useMutation } from "@liveblocks/react/suspense";
function App() {  const addAnimal = useMutation(({ storage }) => {    const animals = storage.get("animals");
    // LiveList<["Fido"]>    console.log(animals);
    animals.push("Felix");
    // LiveList<["Fido", "Felix"]>    console.log(animals);  });
  return <button onClick={addAnimal}>Add animal</button>;}
```

[`useStorage`](/content/docs/api-reference/liveblocks-react#useStorage/index.html) will return an immutable copy of the data, for example a
`LiveList` is converted to an `array`, which makes it easy to render.

```tsx
import { useStorage } from "@liveblocks/react/suspense";
function App() {  const animals = useStorage((root) => root.animals);
  // ["Fido", "Felix"]  console.log(animals);
  return (    <ul>      {animals.map((animal) => (        <li key={animal}>{animal}</li>      ))}    </ul>  );}
```

### [Nesting data structures](/content/docs/api-reference/liveblocks-react\#Nesting-data-structures/index.html)

All Storage data structures can be nested, allowing you to create complex trees
of conflict-free data.

```ts
import { LiveObject, LiveList, LiveMap } from "@liveblocks/client";
type Person = LiveObject<{  name: string;  pets: LiveList<string>;}>;
declare global {  interface Liveblocks {    Storage: {      people: LiveMap<string, Person>;    };  }}
```

Here’s an example of setting `initialStorage` for this type.

```tsx
import { LiveObject, LiveList, LiveMap } from "@liveblocks/client";import { RoomProvider } from "@liveblocks/react/suspense";
function App() {  return (    <RoomProvider      id="my-room-name"      initialStorage={{        people: new LiveMap([          [            "alicia",            new LiveObject({              name: "Alicia",              pets: new LiveList(["Fido", "Felix"]),            }),          ],        ]),      }}    >      {/* children */}    </RoomProvider>  );}
```

Need help troubleshooting Storage?

Get the [Liveblocks DevTools extension](/content/devtools/index.html) to develop and debug your
application as you build it.

### [useStorageRoomProvider](/content/docs/api-reference/liveblocks-react\#useStorage/index.html)

Extracts data from Liveblocks Storage state and automatically subscribes to
updates to that selected data. For full details, see [how selectors work](/content/docs/api-reference/liveblocks-react#selectors/index.html).

```tsx
// ✅ Rerenders if todos (or their children) changeconst items = useStorage((root) => root.todos);
// ✅ Rerenders when todos are added or deletedconst numTodos = useStorage((root) => root.todos.length);
// ✅ Rerenders when the value of allDone changesconst allDone = useStorage((root) => root.todos.every((item) => item.done));
// ✅ Rerenders if any _unchecked_ todo items changeconst uncheckedItems = useStorage(  (root) => root.todos.filter((item) => !item.done),  shallow // 👈);
```

The `root` argument to the `useStorage` selector function is an _immutable_ copy
of your entire Liveblocks Storage tree. Think of it as the value you provided in
the `initialStorage` prop at the [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html) level, but then
(recursively) converted to their “normal” JavaScript equivalents (objects,
arrays, maps) that are read-only.

From that immutable `root`, you can select or compute any value you like. Your
component will automatically get rerendered if the value you return differs from
the last rendered value.

This hook returns `null` while storage is still loading. To avoid that, use the
[Suspense version](/content/docs/api-reference/liveblocks-react#Suspense/index.html).

Avoiding unnecessary rerenders

It’s recommended to select only the subset of Storage data that your component
needs. This will avoid unnecessary rerenders that happen with overselection.

Arguments

- selector(root: ToJson<TStorage>) => T

A selector function to extract specific data from the storage root. The root
is an immutable copy of your entire Liveblocks Storage tree.

- isEqual(prev: T, curr: T) => boolean

Optional equality function to determine if the selected data has changed.
Defaults to strict equality comparison.

Returns

- dataT \| null

The selected data from storage. Returns `null` while storage is still
loading (in non-Suspense version).

### [useHistoryRoomProvider](/content/docs/api-reference/liveblocks-react\#useHistory/index.html)

Returns the room’s history. See [`Room.history`](/content/docs/api-reference/liveblocks-client#Room.history) for more information.

```ts
import { useHistory } from "@liveblocks/react/suspense";
const { undo, redo, pause, resume, disable } = useHistory();
```

Arguments

_None_

Returns

- historyHistory

The room's history object containing methods for
[`undo`](/content/docs/api-reference/liveblocks-client#Room.history.undo),
[`redo`](/content/docs/api-reference/liveblocks-client#Room.history.redo),
[`pause`](/content/docs/api-reference/liveblocks-client#Room.history.pause),
[`resume`](/content/docs/api-reference/liveblocks-client#Room.history.resume), and
[`disable`](/content/docs/api-reference/liveblocks-client#Room.history.disable)
operations.

### [useUndoRoomProvider](/content/docs/api-reference/liveblocks-react\#useUndo/index.html)

Returns a function that undoes the last operation executed by the current
client. It does not impact operations made by other clients.

```ts
import { useUndo } from "@liveblocks/react/suspense";
const undo = useUndo();
```

Arguments

_None_

Returns

- undo() =\> void

A function that undoes the last operation executed by the current client.

### [useRedoRoomProvider](/content/docs/api-reference/liveblocks-react\#useRedo/index.html)

Returns a function that redoes the last operation executed by the current
client. It does not impact operations made by other clients.

```ts
import { useRedo } from "@liveblocks/react/suspense";
const redo = useRedo();
```

Arguments

_None_

Returns

- redo() =\> void

A function that redoes the last operation executed by the current client.

### [useCanUndoRoomProvider](/content/docs/api-reference/liveblocks-react\#useCanUndo/index.html)

Returns whether there are any operations to undo.

```ts
import { useCanUndo, useUpdateMyPresence } from "@liveblocks/react/suspense";
const updateMyPresence = useUpdateMyPresence();const canUndo = useCanUndo();
updateMyPresence({ y: 0 });
// At the next render, "canUndo" will be true
```

Arguments

_None_

Returns

- canUndoboolean

Whether there are any operations to undo.

### [useCanRedoRoomProvider](/content/docs/api-reference/liveblocks-react\#useCanRedo/index.html)

Returns whether there are any operations to redo.

```ts
import {  useCanRedo,  useUndo,  useUpdateMyPresence,} from "@liveblocks/react/suspense";
const updateMyPresence = useUpdateMyPresence();const undo = useUndo();const canRedo = useCanRedo();
updateMyPresence({ y: 0 });undo();
// At the next render, "canRedo" will be true
```

Arguments

_None_

Returns

- canRedoboolean

Whether there are any operations to redo.

### [useMutationRoomProvider](/content/docs/api-reference/liveblocks-react\#useMutation/index.html)

Creates a callback function that lets you mutate Liveblocks state.

```tsx
import { useMutation } from "@liveblocks/react/suspense";
const fillWithRed = useMutation(  // Mutation context is passed as the first argument  ({ storage, setMyPresence }) => {    // Mutate Storage    storage.get("shapes").get("circle1").set("fill", "red");    //                                   ^^^
    // ...or Presence    setMyPresence({ lastUsedColor: "red" });  },  []);
// JSXreturn <button onClick={fillWithRed} />;
```

To make the example above more flexible and work with _any_ color, you have two
options:

1. Close over a local variable and
[adding it to the dependency array](/content/docs/api-reference/liveblocks-react#useMutation-dep-arrays/index.html), or
2. Have it take [an extra callback parameter](/content/docs/api-reference/liveblocks-react#useMutation-extra-params/index.html).

Both are equally fine, just a matter of preference.

Arguments

- mutationFn(context: MutationContext, ...args: TArgs) => void

A function that performs mutations on Liveblocks state. The context provides
access to `storage`, `setMyPresence`, `self`, and `others`.

- depsReact.DependencyList

A dependency array that determines when the mutation function should be
recreated, similar to `useCallback`.

#### [With dependency arrays](/content/docs/api-reference/liveblocks-react\#useMutation-dep-arrays/index.html)

```tsx
// Local state maintained outside Liveblocksconst [currentColor, setCurrentColor] = useState("red");
const fillWithCurrentColor = useMutation(  ({ storage, setMyPresence }) => {    storage.get("shapes").get("circle1").set("fill", currentColor);    setMyPresence({ lastUsedColor: currentColor });  },  [currentColor] // Works just like it would in useCallback);
// JSXreturn <button onClick={fillWithCurrentColor} />;
```

Tip! Let ESLint check your dependencies

If you use ESLint, we recommend to
[configure it to enforce](/content/docs/api-reference/liveblocks-react#useMutation-lint-rule/index.html)
the correct use of your dependency arrays.

#### [With extra callback parameters](/content/docs/api-reference/liveblocks-react\#useMutation-extra-params/index.html)

Alternatively, you can add extra parameters to your callback function:

```tsx
const fill = useMutation(  // Note the second argument  ({ storage, setMyPresence }, color: string) => {    storage.get("shapes").get("circle1").set("fill", color);    setMyPresence({ lastUsedColor: color });  },  []);
// JSXreturn <button onClick={() => fill("red")} />;//                            ^^^^^^^^^^^ Now fill takes a color argument
```

#### [Depending on current presence](/content/docs/api-reference/liveblocks-react\#useMutation-presence/index.html)

For convenience, the mutation context also receives `self` and `others`
arguments, which are _immutable_ values reflecting the current Presence state,
in case your mutation depends on it.

For example, here’s a mutation that will delete all the shapes selected by the
current user.

```tsx
const deleteSelectedShapes = useMutation(  // You can use current "self" or "others" state in the mutation  ({ storage, self, others, setMyPresence }) => {    // Delete the selected shapes    const shapes = storage.get("shapes");    for (const shapeId of self.presence.selectedShapeIds) {      shapes.delete(shapeId);    }
    // Clear the current selection    setMyPresence({ selectedShapeIds: [] });  },  []);
// JSXreturn <button onClick={deleteSelectedShapes} />;
```

Mutations are automatically batched, so when using `useMutation` there’s no need
to use `useBatch`, or call `room.batch()` manually.

#### [ESLint rule](/content/docs/api-reference/liveblocks-react\#useMutation-lint-rule/index.html)

If you are using ESLint in your project, and are using
[the React hooks plugin](https://reactjs.org/docs/hooks-rules.html#eslint-plugin),
we recommend to add a check for "additional hooks", so that it will also check
the dependency arrays of your `useMutation` calls:

```js
{  "rules": {    // ...    "react-hooks/exhaustive-deps": ["warn", {      "additionalHooks": "useMutation"    }]  }}
```

Returns

- mutate(...args: TArgs) => void

A memoized callback function that executes the mutation. Can accept
additional arguments that are passed to the mutation function.

## [Comments](/content/docs/api-reference/liveblocks-react\#Comments/index.html)

### [useThreadsRoomProvider](/content/docs/api-reference/liveblocks-react\#useThreads/index.html)

Returns a paginated list of threads within the current room. Initially fetches
the oldest 50 threads.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useThreads } from "@liveblocks/react";
const { threads, error, isLoading } = useThreads();
```

Use the [`Thread`](/content/docs/api-reference/liveblocks-react-ui#Thread/index.html) component to
render the oldest 50 threads with our default UI.

```tsx
import { Thread } from "@liveblocks/react-ui";import { useThreads } from "@liveblocks/react/suspense";
function Component() {  const { threads } = useThreads();
  return (    <div>      {threads.map((thread) => (        <Thread key={thread.id} thread={thread} />      ))}    </div>  );}
```

Arguments

- optionsobject

Optional configuration object.

- option.queryThreadsQuery

Optional query to filter threads by resolved status and metadata values.
[Learn more](/content/docs/api-reference/liveblocks-react#useThreads-query/index.html).

- option.scrollOnLoadboolean

Whether to scroll to a comment if the URL's hash is set to a comment ID.
Defaults to `true`. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useThreads-scroll/index.html).

Returns

- threadsThreadData\[\] \| undefined

An array of threads within the current room, or `undefined` if not yet
loaded (in non-Suspense version).

- isLoadingboolean

Whether the threads are currently being loaded.

- errorError \| undefined

Any error that occurred while loading the threads.

- hasFetchedAllboolean

Whether all available threads have been fetched. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useThreads-pagination/index.html).

- fetchMore() =\> void

A function to fetch more threads. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useThreads-pagination/index.html).

- isFetchingMoreboolean

Whether more threads are currently being fetched. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useThreads-pagination/index.html).

- fetchMoreErrorError \| undefined

Any error that occurred while fetching more threads. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useThreads-pagination/index.html).

#### [Querying threads](/content/docs/api-reference/liveblocks-react\#useThreads-query/index.html)

It’s possible to return threads that match a certain query with the `query`
option. You can filter threads based on their resolved status, if the user is
subscribed to them, and metadata. Additionally, you can filter for metadata
strings that being with certain characters using `startsWith` and you can filter
for metadata numbers using `gt`, `lt`, `gte`, and `lte`. Returned threads match
the entire query.

```tsx
// Returns threads that match the entire `query`, e.g. { color: "blue", pinned: true, ... }const { threads } = useThreads({  query: {    // Filter for unresolved threads    resolved: false,
    // Filter for threads that the user is subscribed to    subscribed: true,
    metadata: {      // Filter for threads that contain specific string, boolean, and number data      color: "blue",      pinned: true,      priority: 3,
      // Filter for threads with string metadata that starts with a certain value      organization: {        startsWith: "liveblocks:",      },
      // Filter for threads with number metadata that is greater than 50 and lower than 100      posX: {        gt: 50,        lt: 100,      },
      // Filter for threads with number metadata that is greater than or equal to 5      level: {        gte: 5,      },    },  },});
```

#### [Pagination](/content/docs/api-reference/liveblocks-react\#useThreads-pagination/index.html)

By default, the `useThreads` hook returns up to 50 threads. To fetch more, the
hook provides additional fields for pagination, similar to
[`useInboxNotifications`](/content/docs/api-reference/liveblocks-react#useInboxNotifications/index.html).

```tsx
import { useThreads } from "@liveblocks/react";
const {  isLoading,  error,  threads,
  fetchMore,  isFetchingMore,  hasFetchedAll,  fetchMoreError,} = useThreads();
```

- `hasFetchedAll` indicates whether all available threads have been fetched.
- `fetchMore` loads up to 50 more threads, and is always safe to call.
- `isFetchingMore` indicates whether more threads are being fetched.
- `fetchMoreError` returns error statuses resulting from fetching more.

##### [Pagination example](/content/docs/api-reference/liveblocks-react\#useThreads-pagination-example/index.html)

The following example demonstrates how to use the `fetchMore` function to
implement a “Load More” button, which fetches additional threads when clicked.
The button is disabled while fetching is in progress.

```tsx
import { Thread } from "@liveblocks/react-ui";import { useThreads } from "@liveblocks/react/suspense";
function Threads() {  const { threads, hasFetchedAll, fetchMore, isFetchingMore } = useThreads();
  return (    <div>      {threads.map((thread) => (        <Thread key={thread.id} thread={thread} />      ))}      {hasFetchedAll ? (        <div>🎉 You've loaded all threads!</div>      ) : (        <button disabled={isFetchingMore} onClick={fetchMore}>          Load more        </button>      )}    </div>  );}
```

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useThreads-error-handling/index.html)

Error handling is another important aspect to consider when using the
`useThreads` hook. The `error` and `fetchMoreError` fields provide information
about any errors that occurred during the initial fetch or subsequent fetch
operations, respectively. You can use these fields to display appropriate error
messages to the user and implement retry mechanisms if needed.

The following example shows how to display error messages for both initial
loading errors and errors that occur when fetching more threads.

```tsx
import { Thread } from "@liveblocks/react-ui";import { useThreads } from "@liveblocks/react";
function Inbox() {  const { threads, error, fetchMore, fetchMoreError } = useThreads();
  // Handle error if the initial load failed.  // The `error` field is not returned by the Suspense hook as the error is thrown to nearest ErrorBoundary  if (error) {    return (      <div>        <p>Error loading threads: {error.message}</p>      </div>    );  }
  return (    <div>      {threads.map((thread) => (        <Thread key={thread.id} thread={thread} />      ))}
      {fetchMoreError && (        <div>          <p>Error loading more threads: {fetchMoreError.message}</p>          <button onClick={fetchMore}>Retry</button>        </div>      )}    </div>  );}
```

#### [Avoid scrolling to a comment](/content/docs/api-reference/liveblocks-react\#useThreads-scroll/index.html)

By default, `scrollOnLoad`, is enabled. This options scrolls to a comment if the
URL’s hash is set to a comment ID (e.g.
`https://example.com/my-room#cm_nNJs9sb...`), the page will scroll to that
comment once the threads are loaded. To avoid scrolling to a comment, set
`scrollOnLoad` to `false`.

```tsx
const { threads } = useThreads({ scrollOnLoad: false });
```

### [useCreateThreadRoomProvider](/content/docs/api-reference/liveblocks-react\#useCreateThread/index.html)

Returns a function that optimistically creates a thread with an initial comment,
and optionally some thread and comment metadata.

```tsx
import { useCreateThread } from "@liveblocks/react/suspense";
const createThread = useCreateThread();const thread = createThread({  body: {},  attachments: [],  metadata: {},  commentMetadata: {},});
```

Returns

- createThread(options: CreateThreadOptions) => ThreadData

A function that creates a thread with an initial comment, and optionally
thread and comment metadata. Returns the optimistic thread object.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useCreateThread-error-handling/index.html)

`useCreateThread` creates threads optimistically, meaning that a thread object
is returned instantly, before Liveblocks has confirmed a successful thread
creation. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look
for the `CREATE_THREAD_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "CREATE_THREAD_ERROR") {    const { roomId, threadId, commentId, body, metadata } = error.context;    console.log(`Problem creating thread ${threadId}`);  }});
```

### [useSearchCommentsRoomProvider](/content/docs/api-reference/liveblocks-react\#useSearchComments/index.html)

Search comments in the current room using semantic search and various filters.
Returns a list of results, including the ID and the plain text content of
matched comments, and the thread ID of the comment’s parent. Use it to
[create a search bar](/content/docs/api-reference/liveblocks-react#Create-a-comments-search-bar/index.html).

```tsx
import { useSearchComments } from "@liveblocks/react";
const { results, error, isLoading } = useSearchComments({  query: { text: "fruit" },});
// [{ content: "I like apples", threadId: "th_xxx", commentId: "cm_xxx"  }, ...]console.log(results);
```

Its semantic search finds results based on meaning, so a query like “fruit” also
brings up related items such as “apples” or “oranges”, even if the exact words
don’t match.

Arguments

- optionsobject

Configuration object.

- options.querySearchCommentsQuery

Optional query to filter comments by metadata and resolved status of the
parent thread, and presence and absence of attachments and mentions in the
comment.

- options.query.textstringRequired

Text to search within comment content. Uses rich text and vector search for
relevance.

- options.query.threadMetadataPartial<QueryMetadata>

Metadata to filter threads by.

- options.query.threadResolvedboolean

Whether to only return comments from threads that are resolved.

- options.query.hasAttachmentsboolean

Whether to only return comments that have attachments.

- options.query.hasMentionsboolean

Whether to only return comments that have mentions.

Returns

- resultsThreadData\[\] \| undefined

An array of matched comments with the parent thread they belong to.

- isLoadingboolean

Whether the results are currently being loaded.

- errorError \| undefined

Any error that occurred while searching the threads.

#### [Create a comments search bar](/content/docs/api-reference/liveblocks-react\#Create-a-comments-search-bar/index.html)

`useSearchComments` allows you to create a search bar for comments that exist
within the current room. Linking to the `commentId` with a hash will highlight
the comment on the page.

```tsx
import { useSearchComments } from "@liveblocks/react";import { useState } from "react";
function SearchBar() {  const [search, setSearch] = useState("");
  const { results, isLoading, error } = useSearchComments({    query: { text: search },  });
  return (    <>      <input        type="text"        value={search}        onChange={(e) => setSearch(e.target.value)}      />      {isLoading ? (        <div>Loading...</div>      ) : (        results.map((result) => (          <a key={result.commentId} href={"#" + result.commentId}>            {result.content}          </a>        ))      )}    </>  );}
```

You could also add buttons that apply various filters, such as “Resolved” and
“Unresolved”.

Resolved/unresolved filter

### [useDeleteThreadRoomProvider](/content/docs/api-reference/liveblocks-react\#useDeleteThread/index.html)

Returns a function that deletes a thread and all its associated comments by ID.
Only the thread creator can delete the thread.

```tsx
import { useDeleteThread } from "@liveblocks/react/suspense";
const deleteThread = useDeleteThread();deleteThread("th_xxx");
```

Returns

- deleteThread(threadId: string) => void

A function that deletes a thread and all its associated comments by ID.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useDeleteThread-error-handling/index.html)

`useDeleteThread` deletes threads optimistically, meaning that the thread
appears deleted instantly, before Liveblocks has confirmed a successful thread
deletion. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look
for the `DELETE_THREAD_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "DELETE_THREAD_ERROR") {    const { roomId, threadId } = error.context;    console.log(`Problem deleting thread ${threadId}`);  }});
```

### [useEditThreadMetadataRoomProvider](/content/docs/api-reference/liveblocks-react\#useEditThreadMetadata/index.html)

Returns a function that edits a thread’s metadata. To delete an existing
metadata property, set its value to `null`. Passing `undefined` for a metadata
property will ignore it.

```tsx
import { useEditThreadMetadata } from "@liveblocks/react/suspense";
const editThreadMetadata = useEditThreadMetadata();editThreadMetadata({ threadId: "th_xxx", metadata: {} });
```

Returns

- editThreadMetadata(options: { threadId: string; metadata: ThreadMetadata }) => void

A function that edits a thread’s metadata. To delete an existing metadata
property, set its value to `null`.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useEditThreadMetadata-error-handling/index.html)

`useEditThreadMetadata` edits thread metadata optimistically, meaning that the
metadata appears updated instantly, before Liveblocks has confirmed a successful
metadata update. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and
look for the `EDIT_THREAD_METADATA_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "EDIT_THREAD_METADATA_ERROR") {    const { roomId, threadId, metadata } = error.context;    console.log(`Problem editing thread metadata ${threadId}`);  }});
```

### [useMarkThreadAsResolvedRoomProvider](/content/docs/api-reference/liveblocks-react\#useMarkThreadAsResolved/index.html)

Returns a function that marks a thread as resolved.

```tsx
import { useMarkThreadAsResolved } from "@liveblocks/react/suspense";
const markThreadAsResolved = useMarkThreadAsResolved();markThreadAsResolved("th_xxx");
```

Returns

- markThreadAsResolved(threadId: string) => void

A function that marks a thread as resolved.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useMarkThreadAsResolved-error-handling/index.html)

`useMarkThreadAsResolved` marks threads as resolved optimistically, meaning that
the thread appears resolved instantly, before Liveblocks has confirmed the
successful status change. To catch any errors that occur, add
[`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the `MARK_THREAD_AS_RESOLVED_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "MARK_THREAD_AS_RESOLVED_ERROR") {    const { roomId, threadId } = error.context;    console.log(`Problem marking thread as resolved ${threadId}`);  }});
```

### [useMarkThreadAsUnresolvedRoomProvider](/content/docs/api-reference/liveblocks-react\#useMarkThreadAsUnresolved/index.html)

Returns a function that marks a thread as unresolved.

```tsx
import { useMarkThreadAsUnresolved } from "@liveblocks/react/suspense";
const markThreadAsUnresolved = useMarkThreadAsUnresolved();markThreadAsUnresolved("th_xxx");
```

Returns

- markThreadAsUnresolved(threadId: string) => void

A function that marks a thread as unresolved.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useMarkThreadAsUnresolved-error-handling/index.html)

`useMarkThreadAsUnresolved` marks threads as unresolved optimistically, meaning
that the thread appears unresolved instantly, before Liveblocks has confirmed
the successful status change. To catch any errors that occur, add
[`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the `MARK_THREAD_AS_UNRESOLVED_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "MARK_THREAD_AS_UNRESOLVED_ERROR") {    const { roomId, threadId } = error.context;    console.log(`Problem marking thread as unresolved ${threadId}`);  }});
```

### [useMarkThreadAsReadRoomProvider](/content/docs/api-reference/liveblocks-react\#useMarkThreadAsRead/index.html)

Returns a function that marks a thread as read.

```tsx
import { useMarkThreadAsRead } from "@liveblocks/react/suspense";
const markThreadAsRead = useMarkThreadAsRead();markThreadAsRead("th_xxx");
```

Returns

- markThreadAsRead(threadId: string) => void

A function that marks a thread as read.

### [useThreadSubscriptionRoomProvider](/content/docs/api-reference/liveblocks-react\#useThreadSubscription/index.html)

Returns the subscription status of a thread, methods to update it, and when the
thread was last read. The subscription status affects whether the current user
receives inbox notifications when new comments are posted.

```tsx
import { useThreadSubscription } from "@liveblocks/react/suspense";
const { status, subscribe, unsubscribe, unreadSince } =  useThreadSubscription("th_xxx");
```

`subscribe` and `unsubscribe` work similarly to
[`useSubscribeToThread`](/content/docs/api-reference/liveblocks-react#useSubscribeToThread/index.html) and
[`useUnsubscribeFromThread`](/content/docs/api-reference/liveblocks-react#useUnsubscribeFromThread/index.html), but they only affect
the current thread.

Arguments

- threadIdstring

The ID of the thread to get subscription status for.

Returns

- statusThreadSubscriptionStatus

The subscription status of the thread ('subscribed', 'unsubscribed', or
'not\_subscribed').

- subscribe() =\> void

A function to subscribe to the thread.

- unsubscribe() =\> void

A function to unsubscribe from the thread.

- unreadSinceDate \| null

The date when the thread was last read, or null if it has been read.

### [useSubscribeToThreadRoomProvider](/content/docs/api-reference/liveblocks-react\#useSubscribeToThread/index.html)

Returns a function that subscribes the current user to a thread, meaning they
will receive inbox notifications when new comments are posted.

```tsx
import { useSubscribeToThread } from "@liveblocks/react/suspense";
const subscribeToThread = useSubscribeToThread();subscribeToThread("th_xxx");
```

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useSubscribeToThread-error-handling/index.html)

`useSubscribeToThread` subscribes to threads optimistically, meaning that the
subscription appears active instantly, before Liveblocks has confirmed a
successful subscription. To catch any errors that occur, add
[`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the `SUBSCRIBE_TO_THREAD_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "SUBSCRIBE_TO_THREAD_ERROR") {    const { roomId, threadId } = error.context;    console.log(`Problem subscribing to thread ${threadId}`);  }});
```

Subscribing will replace any existing subscription for the current thread
[set at room-level](/content/docs/api-reference/liveblocks-react#useRoomSubscriptionSettings/index.html). This value can also be
overridden by a room-level call that is run afterwards.

```ts
const subscribeToThread = useSubscribeToThread();const [{ settings }, updateSettings] = useRoomSubscriptionSettings();
// 1. Disables notifications for all threadsupdateSettings({  threads: "none",});
// 2. Enables notifications just for this thread, "th_d75sF3..."subscribeToThread("th_d75sF3...");
// 3. Disables notifications for all threads, including "th_d75sF3..."updateSettings({  threads: "none",});
```

Returns

- subscribeToThread(threadId: string) => void

A function that subscribes the current user to a thread for inbox
notifications.

### [useUnsubscribeFromThreadRoomProvider](/content/docs/api-reference/liveblocks-react\#useUnsubscribeFromThread/index.html)

Returns a function that unsubscribes the current user from a thread, meaning
they will no longer receive inbox notifications when new comments are posted.

```tsx
import { useUnsubscribeFromThread } from "@liveblocks/react/suspense";
const unsubscribeFromThread = useUnsubscribeFromThread();unsubscribeFromThread("th_xxx");
```

Unsubscribing will replace any existing subscription for the current thread
[set at room-level](/content/docs/api-reference/liveblocks-react#useRoomSubscriptionSettings/index.html). This value can also be
overridden by a room-level call that is run afterwards.

```ts
const subscribeToThread = useSubscribeToThread();const [{ settings }, updateSettings] = useRoomSubscriptionSettings();
// 1. Enables notifications for all threadsupdateSettings({  threads: "all",});
// 2. Disables notifications just for this thread, "th_d75sF3..."subscribeToThread("th_d75sF3...");
// 3. Enables notifications for all threads, including "th_d75sF3..."updateSettings({  threads: "all",});
```

Returns

- unsubscribeFromThread(threadId: string) => void

A function that unsubscribes the current user from a thread, stopping inbox
notifications.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useUnsubscribeFromThread-error-handling/index.html)

`useUnsubscribeFromThread` unsubscribes from threads optimistically, meaning
that the subscription appears inactive instantly, before Liveblocks has
confirmed a successful unsubscription. To catch any errors that occur, add
[`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the `UNSUBSCRIBE_FROM_THREAD_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "UNSUBSCRIBE_FROM_THREAD_ERROR") {    const { roomId, threadId } = error.context;    console.log(`Problem unsubscribing from thread ${threadId}`);  }});
```

### [useCreateCommentRoomProvider](/content/docs/api-reference/liveblocks-react\#useCreateComment/index.html)

Returns a function that adds a comment to a thread.

```tsx
import { useCreateComment } from "@liveblocks/react/suspense";
const createComment = useCreateComment();const comment = createComment({  threadId: "th_xxx",  body: {},  attachments: [],  metadata: {},});
```

Returns

- createComment(options: CreateCommentOptions) => CommentData

A function that adds a comment to a thread. Returns the optimistic comment
object.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useCreateComment-error-handling/index.html)

`useCreateComment` creates comments optimistically, meaning that a comment
object is returned instantly, before Liveblocks has confirmed a successful
comment creation. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and
look for the `CREATE_COMMENT_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "CREATE_COMMENT_ERROR") {    const { roomId, threadId, commentId, body } = error.context;    console.log(`Problem creating comment ${commentId}`);  }});
```

### [useEditCommentRoomProvider](/content/docs/api-reference/liveblocks-react\#useEditComment/index.html)

Returns a function that edits a comment’s body, and optionally its attachments
and metadata.

```tsx
import { useEditComment } from "@liveblocks/react/suspense";
const editComment = useEditComment();editComment({  threadId: "th_xxx",  commentId: "cm_xxx",  body: {},  attachments: [],  metadata: {},});
```

Returns

- editComment(options: EditCommentOptions) => void

A function that edits a comment’s body, and optionally its attachments and
metadata.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useEditComment-error-handling/index.html)

`useEditComment` edits comments optimistically, meaning that the comment appears
updated instantly, before Liveblocks has confirmed a successful comment edit. To
catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the
`EDIT_COMMENT_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "EDIT_COMMENT_ERROR") {    const { roomId, threadId, commentId, body } = error.context;    console.log(`Problem editing comment ${commentId}`);  }});
```

### [useEditCommentMetadataRoomProvider](/content/docs/api-reference/liveblocks-react\#useEditCommentMetadata/index.html)

Returns a function that edits a comment’s metadata. To delete an existing
metadata property, set its value to `null`. Passing `undefined` for a metadata
property will ignore it.

```tsx
import { useEditCommentMetadata } from "@liveblocks/react/suspense";
const editCommentMetadata = useEditCommentMetadata();editCommentMetadata({  threadId: "th_xxx",  commentId: "cm_xxx",  metadata: {},});
```

Returns

- editCommentMetadata(options: { threadId: string; commentId: string; metadata: CommentMetadata }) => void

A function that edits a comment’s metadata. To delete an existing metadata
property, set its value to `null`.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useEditCommentMetadata-error-handling/index.html)

`useEditCommentMetadata` edits comment metadata optimistically, meaning that the
metadata appears updated instantly, before Liveblocks has confirmed a successful
metadata update. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and
look for the `EDIT_COMMENT_METADATA_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "EDIT_COMMENT_METADATA_ERROR") {    const { roomId, threadId, commentId, metadata } = error.context;    console.log(`Problem editing comment metadata ${commentId}`);  }});
```

### [useDeleteCommentRoomProvider](/content/docs/api-reference/liveblocks-react\#useDeleteComment/index.html)

Returns a function that deletes a comment. If it is the last non-deleted
comment, the thread also gets deleted.

```tsx
import { useDeleteComment } from "@liveblocks/react/suspense";
const deleteComment = useDeleteComment();deleteComment({ threadId: "th_xxx", commentId: "cm_xxx" });
```

Returns

- deleteComment(options: { threadId: string; commentId: string }) => void

A function that deletes a comment. If it is the last non-deleted comment,
the thread also gets deleted.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useDeleteComment-error-handling/index.html)

`useDeleteComment` deletes comments optimistically, meaning that the comment
appears deleted instantly, before Liveblocks has confirmed a successful comment
deletion. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look
for the `DELETE_COMMENT_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "DELETE_COMMENT_ERROR") {    const { roomId, threadId, commentId } = error.context;    console.log(`Problem deleting comment ${commentId}`);  }});
```

### [useAddReactionRoomProvider](/content/docs/api-reference/liveblocks-react\#useAddReaction/index.html)

Returns a function that adds a reaction to a comment. Can be used to create an
[emoji picker](/content/docs/api-reference/liveblocks-react-ui#emoji-picker/index.html) or
[emoji reactions](/content/docs/api-reference/liveblocks-react-ui#emoji-reactions/index.html).

```tsx
import { useAddReaction } from "@liveblocks/react/suspense";
const addReaction = useAddReaction();addReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" });
```

Returns

- addReaction(options: { threadId: string; commentId: string; emoji: string }) => void

A function that adds a reaction to a comment.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useAddReaction-error-handling/index.html)

`useAddReaction` adds reactions optimistically, meaning that the reaction
appears instantly, before Liveblocks has confirmed a successful reaction
addition. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look
for the `ADD_REACTION_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "ADD_REACTION_ERROR") {    const { roomId, threadId, commentId, emoji } = error.context;    console.log(`Problem adding reaction ${emoji} to comment ${commentId}`);  }});
```

### [useRemoveReactionRoomProvider](/content/docs/api-reference/liveblocks-react\#useRemoveReaction/index.html)

Returns a function that removes a reaction from a comment. Can be used to create
an [emoji picker](/content/docs/api-reference/liveblocks-react-ui#emoji-picker/index.html) or
[emoji reactions](/content/docs/api-reference/liveblocks-react-ui#emoji-reactions/index.html)

```tsx
import { useRemoveReaction } from "@liveblocks/react/suspense";
const removeReaction = useRemoveReaction();removeReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" });
```

Returns

- removeReaction(options: { threadId: string; commentId: string; emoji: string }) => void

A function that removes a reaction from a comment.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useRemoveReaction-error-handling/index.html)

`useRemoveReaction` removes reactions optimistically, meaning that the reaction
disappears instantly, before Liveblocks has confirmed a successful reaction
removal. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for
the `REMOVE_REACTION_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "REMOVE_REACTION_ERROR") {    const { roomId, threadId, commentId, emoji } = error.context;    console.log(`Problem removing reaction ${emoji} from comment ${commentId}`);  }});
```

### [useAttachmentUrlRoomProvider](/content/docs/api-reference/liveblocks-react\#useAttachmentUrl/index.html)

Returns a presigned URL for an attachment by its ID.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useAttachmentUrl } from "@liveblocks/react";
const { url, error, isLoading } = useAttachmentUrl("at_xxx");
```

Arguments

- attachmentIdstring

The ID of the attachment to get a presigned URL for.

Returns

- urlstring \| undefined

The presigned URL for the attachment, or `undefined` if not found or not yet
loaded (in non-Suspense version).

- isLoadingboolean

Whether the URL is currently being loaded.

- errorError \| undefined

Any error that occurred while loading the URL.

## [Feeds](/content/docs/api-reference/liveblocks-react\#Feeds/index.html)

### [useFeedsRoomProvider](/content/docs/api-reference/liveblocks-react\#useFeeds/index.html)

Returns a paginated list of feeds within the current room. Results are sorted
oldest first and can be [filtered](/content/docs/api-reference/liveblocks-react#useFeeds-filtering/index.html) and
[paginated](/content/docs/api-reference/liveblocks-react#useFeeds-pagination/index.html).
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useFeeds } from "@liveblocks/react";
const { feeds, error, isLoading } = useFeeds();
```

Arguments

- optionsobject

Optional configuration object.

- options.sincenumber

Optional timestamp filter (ms). Only feeds whose `createdAt` or `updatedAt`
is at or after this value are included in `feeds` (applied to the cached
data for this hook’s options).

- options.metadataRecord<string, string>

Optional metadata filter. Only feeds whose metadata matches every key/value
pair are included (applied to the cached data for this hook’s options).

- options.limitnumber

Page size for each server request when loading or loading more feeds. Does
not cap how many feeds appear in `feeds`—use `fetchMore` until
`hasFetchedAll` is true.

Returns

- feedsFeed\[\] \| undefined

An array of feeds within the current room matching `since` and `metadata`
for this call, sorted by `createdAt` ascending (tie-break on `feedId`), or
`undefined` if not yet loaded (in non-Suspense version).

- isLoadingboolean

Whether the feeds are currently being loaded.

- errorError \| undefined

Any error that occurred while loading the feeds.

- hasFetchedAllboolean

Whether all available feeds have been fetched.

- fetchMore() =\> void

A function to fetch more feeds.

- isFetchingMoreboolean

Whether more feeds are currently being fetched.

- fetchMoreErrorError \| undefined

Any error that occurred while fetching more feeds.

#### [Filtering](/content/docs/api-reference/liveblocks-react\#useFeeds-filtering/index.html)

It’s possible to filter feeds by timestamp and metadata, and results are merged
into a per-room cache. Limit sets the page size for each fetch when
[paginating](/content/docs/api-reference/liveblocks-react/index.html).

```tsx
const { feeds } = useFeeds({  // Optional, fetch feeds from the last day  since: Date.now() - 1000 * 60 * 60 * 24,
  // Optional, fetch feeds with the `{ tag: "design" }` metadata  metadata: {    tag: "design",  },
  // Optional, fetch only 10 at a time  limit: 10,});
```

#### [Pagination](/content/docs/api-reference/liveblocks-react\#useFeeds-pagination/index.html)

By default, the `useFeeds` hook returns up to 50 feeds. To fetch more, the hook
provides additional fields for pagination, similar to [`useThreads`](/content/docs/api-reference/liveblocks-react#useThreads/index.html).

```tsx
import { useFeeds } from "@liveblocks/react";
const {  feeds,  error,  isLoading,
  fetchMore,  isFetchingMore,  hasFetchedAll,  fetchMoreError,} = useFeeds();
```

- `hasFetchedAll` indicates whether all available feeds have been fetched.
- `fetchMore` loads up to 50 more feeds, and is always safe to call.
- `isFetchingMore` indicates whether more feeds are being fetched.
- `fetchMoreError` returns error statuses resulting from fetching more.

##### [Pagination example](/content/docs/api-reference/liveblocks-react\#useFeeds-pagination-example/index.html)

The following example demonstrates how to use the `fetchMore` function to
implement a “Load More” button, which fetches additional feeds when clicked. The
button is disabled while fetching is in progress.

```tsx
import { Feed } from "@liveblocks/react-ui";import { useFeeds } from "@liveblocks/react/suspense";
function Feeds() {  const { feeds, hasFetchedAll, fetchMore, isFetchingMore } = useFeeds();
  return (    <div>      {feeds.map((feed) => (        <a key={feed.feedId} href={`/feeds/${feed.feedId}`}>          {feed.metadata.name}        </a>      ))}      {hasFetchedAll ? (        <div>🎉 You've loaded all feeds!</div>      ) : (        <button disabled={isFetchingMore} onClick={fetchMore}>          Load more        </button>      )}    </div>  );}
```

### [useFeedMessagesRoomProvider](/content/docs/api-reference/liveblocks-react\#useFeedMessages/index.html)

Returns a paginated list of messages for a specific feed in the current room.
Messages are sorted newest first using their `createdAt` property and can be
[paginated](/content/docs/api-reference/liveblocks-react#useFeedMessages-pagination/index.html).
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useFeedMessages } from "@liveblocks/react";
const { messages, error, isLoading } = useFeedMessages("my-feed-id");
```

Arguments

- feedIdstringRequired

The ID of the feed to get messages from.

- optionsobject

Optional configuration object.

- options.cursorstring

Optional cursor for pagination.

- options.limitnumber

Page size for each server request when loading or loading more messages.
Does not cap how many messages appear in `messages`—use `fetchMore` until
`hasFetchedAll` is true.

Returns

- messagesFeedMessage\[\] \| undefined

An array of messages in the feed (chronological order by `createdAt`), or
`undefined` if not yet loaded (in non-Suspense version).

- isLoadingboolean

Whether the messages are currently being loaded.

- errorError \| undefined

Any error that occurred while loading the messages.

- hasFetchedAllboolean

Whether all available messages have been fetched.

- fetchMore() =\> void

A function to fetch more messages.

- isFetchingMoreboolean

Whether more messages are currently being fetched.

- fetchMoreErrorError \| undefined

Any error that occurred while fetching more messages.

#### [Pagination](/content/docs/api-reference/liveblocks-react\#useFeedMessages-pagination/index.html)

By default, the `useFeedMessages` hook returns up to 50 messages. To fetch more,
the hook provides additional fields for pagination, similar to [`useThreads`](/content/docs/api-reference/liveblocks-react#useThreads/index.html).

```tsx
import { useFeedMessages } from "@liveblocks/react";
const {  messages,  error,  isLoading,
  hasFetchedAll,  fetchMore,  isFetchingMore,  fetchMoreError,} = useFeedMessages("my-feed-id");
```

- `hasFetchedAll` indicates whether all available messages have been fetched.
- `fetchMore` loads up to 50 more messages, and is always safe to call.
- `isFetchingMore` indicates whether more messages are being fetched.
- `fetchMoreError` returns error statuses resulting from fetching more.

##### [Pagination example](/content/docs/api-reference/liveblocks-react\#useFeedMessages-pagination-example/index.html)

The following example demonstrates how to use the `fetchMore` function to
implement a “Load More” button, which fetches additional messages when clicked.
The button is disabled while fetching is in progress.

```tsx
import { useFeedMessages } from "@liveblocks/react/suspense";
function FeedMessages({ feedId }: { feedId: string }) {  const { messages, hasFetchedAll, fetchMore, isFetchingMore } =    useFeedMessages(feedId);
  return (    <div>      {messages.map((message) => (        <div key={message.id}>{message.data.content}</div>      ))}      {hasFetchedAll ? (        <div>🎉 You've loaded all messages!</div>      ) : (        <button disabled={isFetchingMore} onClick={fetchMore}>          Load more        </button>      )}    </div>  );}
```

### [useCreateFeedRoomProvider](/content/docs/api-reference/liveblocks-react\#useCreateFeed/index.html)

Returns a function that creates a new feed in the current room.

```tsx
import { useCreateFeed } from "@liveblocks/react";
const createFeed = useCreateFeed();createFeed("my-feed-id", {  metadata: { name: "My Feed", channel: true },  timestamp: Date.now(),});
```

Returns

- createFeed(feedId: string, options?: { metadata?: JsonObject; timestamp?: number }) => void

A function that creates a feed. Takes the feed ID and optional metadata and
timestamp.

### [useDeleteFeedRoomProvider](/content/docs/api-reference/liveblocks-react\#useDeleteFeed/index.html)

Returns a function that deletes a feed from the current room.

```tsx
import { useDeleteFeed } from "@liveblocks/react";
const deleteFeed = useDeleteFeed();deleteFeed("my-feed-id");
```

Returns

- deleteFeed(feedId: string) => void

A function that deletes a feed. Takes the feed ID.

### [useUpdateFeedMetadataRoomProvider](/content/docs/api-reference/liveblocks-react\#useUpdateFeedMetadata/index.html)

Returns a function that updates a feed's metadata in the current room.

```tsx
import { useUpdateFeedMetadata } from "@liveblocks/react";
const updateFeedMetadata = useUpdateFeedMetadata();updateFeedMetadata("my-feed-id", {  name: "Updated Name",  updated: new Date().toISOString(),});
```

Returns

- updateFeedMetadata(feedId: string, metadata: JsonObject) => void

A function that updates a feed's metadata. Takes the feed ID and the new
metadata object.

### [useCreateFeedMessageRoomProvider](/content/docs/api-reference/liveblocks-react\#useCreateFeedMessage/index.html)

Returns a function that adds a new message to a feed in the current room.

```tsx
import { useCreateFeedMessage } from "@liveblocks/react";
const createFeedMessage = useCreateFeedMessage();createFeedMessage("my-feed-id", { role: "user", content: "Hello, world!" });
// With optional id and timestampcreateFeedMessage(  "my-feed-id",  { role: "user", content: "Hello!" },  {    id: "my-message-id",    timestamp: Date.now(),  });
```

Returns

- createFeedMessage(feedId: string, data: JsonObject, options?: { id?: string; timestamp?: number }) => void

A function that adds a message to a feed. Takes the feed ID, message data,
and optional id and timestamp.

### [useDeleteFeedMessageRoomProvider](/content/docs/api-reference/liveblocks-react\#useDeleteFeedMessage/index.html)

Returns a function that deletes a message from a feed in the current room.

```tsx
import { useDeleteFeedMessage } from "@liveblocks/react";
const deleteFeedMessage = useDeleteFeedMessage();deleteFeedMessage("my-feed-id", "my-message-id");
```

Returns

- deleteFeedMessage(feedId: string, messageId: string) => void

A function that deletes a feed message. Takes the feed ID and message ID.

### [useUpdateFeedMessageRoomProvider](/content/docs/api-reference/liveblocks-react\#useUpdateFeedMessage/index.html)

Returns a function that updates a feed message in the current room.

```tsx
import { useUpdateFeedMessage } from "@liveblocks/react";
const updateFeedMessage = useUpdateFeedMessage();updateFeedMessage("my-feed-id", "my-message-id", {  role: "user",  content: "Updated content",});
```

Returns

- updateFeedMessage(feedId: string, messageId: string, data: JsonObject) => void

A function that updates a feed message. Takes the feed ID, message ID, and
the new data object.

#### [Typing feed metadata and message data](/content/docs/api-reference/liveblocks-react\#feeds-typescript/index.html)

You can type feed metadata and feed message data using the `Liveblocks`
interface in your `liveblocks.config.ts` file. This provides type safety for
[`useFeeds`](/content/docs/api-reference/liveblocks-react#useFeeds/index.html) and [`useFeedMessages`](/content/docs/api-reference/liveblocks-react#useFeedMessages/index.html).

```ts
// liveblocks.config.tsdeclare global {  interface Liveblocks {    FeedMetadata: {      name?: string;      channel?: boolean;      agentName?: string;    };
    FeedMessageData: {      role: "user" | "assistant" | "system";      content: string;    };  }}
```

## [Notifications](/content/docs/api-reference/liveblocks-react\#Notifications/index.html)

### [useInboxNotificationsLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useInboxNotifications/index.html)

Returns a paginated list of inbox notifications for the current user. Initially
fetches the latest 50 items. Inbox notifications are
[project-based](/content/docs/ready-made-features/notifications/concepts#Project-based/index.html),
meaning notifications from outside the current room are received.

```tsx
import { useInboxNotifications } from "@liveblocks/react";
const { inboxNotifications, error, isLoading } = useInboxNotifications();
```

Use the
[`InboxNotification`](/content/docs/api-reference/liveblocks-react-ui#InboxNotification/index.html)
component to render the latest 50 inbox notifications with our default UI.

```tsx
import { InboxNotification } from "@liveblocks/react-ui";import { useInboxNotifications } from "@liveblocks/react/suspense";
function Inbox() {  const { inboxNotifications } = useInboxNotifications();
  return (    <div>      {inboxNotifications.map((notification) => (        <InboxNotification          key={notification.id}          inboxNotification={notification}        />      ))}    </div>  );}
```

Arguments

- optionsobject

Optional configuration object.

- options.queryInboxNotificationsQuery

Optional query to filter notifications by room ID or kind. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useInboxNotifications-query/index.html).

Returns

- inboxNotificationsInboxNotificationData\[\] \| undefined

An array of inbox notifications for the current user, or `undefined` if not
yet loaded (in non-Suspense version).

- isLoadingboolean

Whether the notifications are currently being loaded.

- errorError \| undefined

Any error that occurred while loading the notifications.

- hasFetchedAllboolean

Whether all available notifications have been fetched. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useInboxNotifications-pagination/index.html).

- fetchMore() =\> void

A function to fetch more notifications. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useInboxNotifications-pagination/index.html).

- isFetchingMoreboolean

Whether more notifications are currently being fetched. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useInboxNotifications-pagination/index.html).

- fetchMoreErrorError \| undefined

Any error that occurred while fetching more notifications. [Learn\\
more](/content/docs/api-reference/liveblocks-react#useInboxNotifications-pagination/index.html).

#### [Querying inbox notifications](/content/docs/api-reference/liveblocks-react\#useInboxNotifications-query/index.html)

It’s possible to return inbox notifications that match a certain query with the
`query` option. You can filter inbox notifications based on their associated
room ID or kind.

```tsx
// Returns inbox notifications that match the entire `query`, e.g. { roomId: "room1", ... }const { inboxNotifications } = useInboxNotifications({  query: {    // Filter for roomId    roomId: "room1",    // Filter for kind    kind: "thread",  },});
```

#### [Pagination](/content/docs/api-reference/liveblocks-react\#useInboxNotifications-pagination/index.html)

By default, the `useInboxNotifications` hook returns up to 50 notifications. To
fetch more, the hook provides additional fields for pagination, similar to
[`useThreads`](/content/docs/api-reference/liveblocks-react#useThreads/index.html).

```tsx
import { useInboxNotifications } from "@liveblocks/react";
const {  inboxNotifications,  isLoading,  error,
  hasFetchedAll,  fetchMore,  isFetchingMore,  fetchMoreError,} = useInboxNotifications();
```

- `hasFetchedAll` indicates whether all available inbox notifications have been
fetched.
- `fetchMore` loads up to 50 more notifications, and is always safe to call.
- `isFetchingMore` indicates whether more notifications are being fetched.
- `fetchMoreError` returns error statuses resulting from fetching more.

##### [Pagination example](/content/docs/api-reference/liveblocks-react\#useInboxNotifications-pagination-example/index.html)

The following example demonstrates how to use the `fetchMore` function to
implement a “Load More” button, which fetches additional inbox notifications
when clicked. The button is disabled while fetching is in progress.

```tsx
import { InboxNotification } from "@liveblocks/react-ui";import { useInboxNotifications } from "@liveblocks/react/suspense";
function Inbox() {  const { inboxNotifications, hasFetchedAll, fetchMore, isFetchingMore } =    useInboxNotifications();
  return (    <div>      {inboxNotifications.map((notification) => (        <InboxNotification          key={notification.id}          inboxNotification={notification}        />      ))}      {hasFetchedAll ? (        <div>🎉 You're all caught up!</div>      ) : (        <button disabled={isFetchingMore} onClick={fetchMore}>          Load more        </button>      )}    </div>  );}
```

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useInboxNotifications-error-handling/index.html)

Error handling is another important aspect to consider when using the
`useInboxNotifications` hook. The `error` and `fetchMoreError` fields provide
information about any errors that occurred during the initial fetch or
subsequent fetch operations, respectively. You can use these fields to display
appropriate error messages to the user and implement retry mechanisms if needed.

The following example shows how to display error messages for both initial
loading errors and errors that occur when fetching more inbox notifications.

```tsx
import { InboxNotification } from "@liveblocks/react-ui";import { useInboxNotifications } from "@liveblocks/react";
function Inbox() {  const { inboxNotifications, error, fetchMore, fetchMoreError } =    useInboxNotifications();
  // Handle error if the initial load failed.  // The `error` field is not returned by the Suspense hook as the error is thrown to nearest ErrorBoundary  if (error) {    return (      <div>        <p>Error loading inbox notifications: {error.message}</p>      </div>    );  }
  return (    <div>      {inboxNotifications.map((notification) => (        <InboxNotification          key={notification.id}          inboxNotification={notification}        />      ))}
      {fetchMoreError && (        <div>          <p>            Error loading more inbox notifications: {fetchMoreError.message}          </p>          <button onClick={fetchMore}>Retry</button>        </div>      )}    </div>  );}
```

#### [Batched notifications](/content/docs/api-reference/liveblocks-react\#useInboxNotifications-batched-notifications/index.html)

If you’re
[batching custom notifications](/content/docs/api-reference/liveblocks-node#Batching-custom-notifications/index.html),
you can render each activity inside a single notification. The `activities`
array will contain multiple items.

```tsx
import { InboxNotification } from "@liveblocks/react-ui";import { useInboxNotifications } from "@liveblocks/react/suspense";
function Inbox() {  const { inboxNotifications } = useInboxNotifications();
  // If the last notification was batched, it will have multiple  // items in the `activities` array  // {  //   id: "in_3dH7sF3...",  //   kind: "$fileUploaded",  //   activities: [  //     { status: "processing" },  //     { status: "complete" },  //   ],  //   ...  // }  console.log(inboxNotifications[0].activities);
  // ...}
```

### [useUnreadInboxNotificationsCountLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useUnreadInboxNotificationsCount/index.html)

Returns the number of unread inbox notifications for the current user.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useUnreadInboxNotificationsCount } from "@liveblocks/react";
const { count, error, isLoading } = useUnreadInboxNotificationsCount();
```

Arguments

- optionsUseUnreadInboxNotificationsCountOptions

Optional configuration object.

Options

- queryInboxNotificationsQuery

Optional query to filter notifications count by room ID or kind.

Returns

- countnumber \| undefined

The number of unread inbox notifications for the current user, or
`undefined` if not yet loaded (in non-Suspense version).

- isLoadingboolean

Whether the number of unread inbox notifications is currently being loaded.

- errorError \| undefined

Any error that occurred while loading the number of unread inbox
notifications.

#### [Querying unread inbox notifications count](/content/docs/api-reference/liveblocks-react\#Querying-unread-inbox-notifications-count/index.html)

It’s possible to return the count of unread inbox notifications that match a
certain query with the `query` option. You can filter unread inbox notifications
count based on their associated room ID or kind.

```tsx
// Returns the count that match the entire `query`, e.g. { roomId: "room1", ... }const { count } = useUnreadInboxNotificationsCount({  query: {    // Filter for roomId    roomId: "room1",    // Filter for kind    kind: "thread",  },});
```

### [useMarkInboxNotificationAsReadLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useMarkInboxNotificationAsRead/index.html)

Returns a function that marks an inbox notification as read for the current
user.

```tsx
import { useMarkInboxNotificationAsRead } from "@liveblocks/react/suspense";
const markInboxNotificationAsRead = useMarkInboxNotificationAsRead();markInboxNotificationAsRead("in_xxx");
```

Returns

- markInboxNotificationAsRead(inboxNotificationId: string) => void

A function that marks an inbox notification as read for the current user.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useMarkInboxNotificationAsRead-error-handling/index.html)

`useMarkInboxNotificationAsRead` marks notifications as read optimistically,
meaning that the notification appears read instantly, before Liveblocks has
confirmed a successful status change. To catch any errors that occur, add
[`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the `MARK_INBOX_NOTIFICATION_AS_READ_ERROR`
type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "MARK_INBOX_NOTIFICATION_AS_READ_ERROR") {    const { inboxNotificationId, roomId } = error.context;    console.log(`Problem marking notification as read ${inboxNotificationId}`);  }});
```

### [useMarkAllInboxNotificationsAsReadLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useMarkAllInboxNotificationsAsRead/index.html)

Returns a function that marks all of the current user‘s inbox notifications as
read.

```tsx
import { useMarkAllInboxNotificationsAsRead } from "@liveblocks/react/suspense";
const markAllInboxNotificationsAsRead = useMarkAllInboxNotificationsAsRead();markAllInboxNotificationsAsRead();
```

Returns

- markAllInboxNotificationsAsRead() =\> void

A function that marks all of the current user's inbox notifications as read.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useMarkAllInboxNotificationsAsRead-error-handling/index.html)

`useMarkAllInboxNotificationsAsRead` marks all notifications as read
optimistically, meaning that the notifications appear read instantly, before
Liveblocks has confirmed a successful status change. To catch any errors that
occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the
`MARK_ALL_INBOX_NOTIFICATIONS_AS_READ_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "MARK_ALL_INBOX_NOTIFICATIONS_AS_READ_ERROR") {    console.log("Problem marking all notifications as read");  }});
```

### [useDeleteInboxNotificationLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useDeleteInboxNotification/index.html)

Returns a function that deletes an inbox notification for the current user.

```tsx
import { useDeleteInboxNotification } from "@liveblocks/react/suspense";
const deleteInboxNotification = useDeleteInboxNotification();deleteInboxNotification("in_xxx");
```

Returns

- deleteInboxNotification(inboxNotificationId: string) => void

A function that deletes an inbox notification for the current user.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useDeleteInboxNotification-error-handling/index.html)

`useDeleteInboxNotification` deletes notifications optimistically, meaning that
the notification appears deleted instantly, before Liveblocks has confirmed a
successful deletion. To catch any errors that occur, add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html)
and look for the `DELETE_INBOX_NOTIFICATION_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "DELETE_INBOX_NOTIFICATION_ERROR") {    const { inboxNotificationId } = error.context;    console.log(`Problem deleting notification ${inboxNotificationId}`);  }});
```

### [useDeleteAllInboxNotificationsLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useDeleteAllInboxNotifications/index.html)

Returns a function that deletes all of the current user‘s inbox notifications.

```tsx
import { useDeleteAllInboxNotifications } from "@liveblocks/react/suspense";
const deleteAllInboxNotifications = useDeleteAllInboxNotifications();deleteAllInboxNotifications();
```

Returns

- deleteAllInboxNotifications() =\> void

A function that deletes all of the current user’s inbox notifications.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useDeleteAllInboxNotifications-error-handling/index.html)

`useDeleteAllInboxNotifications` deletes all notifications optimistically,
meaning that the notifications appear deleted instantly, before Liveblocks has
confirmed a successful deletion. To catch any errors that occur, add
[`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the `DELETE_ALL_INBOX_NOTIFICATIONS_ERROR`
type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "DELETE_ALL_INBOX_NOTIFICATIONS_ERROR") {    console.log("Problem deleting all notifications");  }});
```

### [useInboxNotificationThreadLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useInboxNotificationThread/index.html)

Returns the thread associated with a `"thread"` inbox notification.

```tsx
import { useInboxNotificationThread } from "@liveblocks/react/suspense";
const thread = useInboxNotificationThread("in_xxx");
```

It can **only** be called with IDs of `"thread"` inbox notifications, so we
recommend only using it
[when customizing the rendering](/content/docs/api-reference/liveblocks-react-ui#Rendering-notification-kinds-differently/index.html)
or in other situations where you can guarantee the kind of the notification.

No fetching and waterfalls

When `useInboxNotifications` returns `"thread"` inbox notifications, it also
receives the associated threads and caches them behind the scenes. When you call
`useInboxNotificationThread`, it simply returns the cached thread for the inbox
notification ID you passed to it, without any fetching or waterfalls.

Arguments

- inboxNotificationIdstring

The ID of the inbox notification to get the associated thread for. Must be a
"thread" type notification.

Returns

- threadThreadData \| null

The thread associated with the inbox notification, or null if not found.

### [useRoomSubscriptionSettingsRoomProvider](/content/docs/api-reference/liveblocks-react\#useRoomSubscriptionSettings/index.html)

Returns the user’s subscription settings for the current room and a function to
update them. Updating this setting will change which
[`inboxNotifications`](/content/docs/api-reference/liveblocks-react#useInboxNotifications/index.html) the current user receives in the
current room.

```tsx
import { useRoomSubscriptionSettings } from "@liveblocks/react/suspense";
const [{ settings }, updateSettings] = useRoomSubscriptionSettings();
// { threads: "replies_and_mentions", textMentions: "mine" }console.log(settings);
// No longer receive thread subscriptions in this roomupdateSettings({  threads: "none",});
```

For `"threads"`, these are the three possible values that can be set:

- `"all"` Receive notifications for every activity in every thread.
- `"replies_and_mentions"` Receive notifications for mentions and threads you’re
participating in.
- `"none"` No notifications are received.

For `"textMentions"`, these are the two possible values that can be set:

- `"mine"` Receive notifications for mentions of you.
- `"none"` No notifications are received.

Returns

- settingsRoomSubscriptionSettings

The current subscription settings for the room.

- updateSettings(settings: Partial<RoomSubscriptionSettings>) => void

A function to update the subscription settings.

### [useUpdateRoomSubscriptionSettingsRoomProvider](/content/docs/api-reference/liveblocks-react\#useUpdateRoomSubscriptionSettings/index.html)

Returns a function that updates the user’s notification settings for the current
room. Updating this setting will change which
[`inboxNotifications`](/content/docs/api-reference/liveblocks-react#useInboxNotifications/index.html) the current user receives in the
current room.

```tsx
import { useUpdateRoomSubscriptionSettings } from "@liveblocks/react/suspense";
const updateRoomSubscriptionSettings = useUpdateRoomSubscriptionSettings();
// No longer receive thread notifications in this roomupdateSettings({  threads: "none",});
```

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useUpdateRoomSubscriptionSettings-error-handling/index.html)

`useUpdateRoomSubscriptionSettings` updates subscription settings
optimistically, meaning that the settings appear updated instantly, before
Liveblocks has confirmed a successful update. To catch any errors that occur,
add [`useErrorListener`](/content/docs/api-reference/liveblocks-react#useErrorListener/index.html) and look for the
`UPDATE_ROOM_SUBSCRIPTION_SETTINGS_ERROR` type.

```tsx
import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => {  if (error.context.type === "UPDATE_ROOM_SUBSCRIPTION_SETTINGS_ERROR") {    const { roomId } = error.context;    console.log(`Problem updating subscription settings for room ${roomId}`);  }});
```

For `"threads"`, these are the three possible values that can be set:

- `"all"` Receive notifications for every activity in every thread.
- `"replies_and_mentions"` Receive notifications for mentions and threads you're
participating in.
- `"none"` No notifications are received.

For `"textMentions"`, these are the two possible values that can be set:

- `"mine"` Receive notifications for mentions of you.
- `"none"` No notifications are received.

Works the same as `updateSettings` in
[`useRoomSubscriptionSettings`](/content/docs/api-reference/liveblocks-react#useRoomSubscriptionSettings/index.html).

Returns

- updateRoomSubscriptionSettings(settings: Partial<RoomSubscriptionSettings>) => void

A function that updates the user's notification settings for the current
room.

##### [Replacing individual thread subscriptions](/content/docs/api-reference/liveblocks-react\#Replacing-individual-thread-subscriptions/index.html)

Subscribing will replace any
[existing thread subscriptions](/content/docs/api-reference/liveblocks-react#useSubscribeToThread/index.html) in the current room. This
value can also be overridden by a room-level call that is run afterwards.

```ts
const subscribeToThread = useSubscribeToThread();const [{ settings }, updateSettings] = useRoomSubscriptionSettings();
// 1. Enables notifications just for this thread, "th_d75sF3..."subscribeToThread("th_d75sF3...");
// 2. Disables notifications for all threads, including "th_d75sF3..."updateSettings({  threads: "none",});
```

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useUpdateRoomSubscriptionSettings-error-handling/index.html)

### [useNotificationSettingsLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useNotificationSettings/index.html)

Notification settings is currently in beta.

Returns the user’s notification settings in the current project, in other words
which [notification webhook events](/content/docs/platform/webhooks#NotificationEvent/index.html)
will be sent for the current user. Notification settings are project-based,
which means that `settings` and `updateSettings` are for the current user’s
settings in every room. Useful for creating a
[notification settings panel](/content/docs/guides/how-to-create-a-notification-settings-panel/index.html).

```tsx
import { useNotificationSettings } from "@liveblocks/react";
const [{ isLoading, error, settings }, updateSettings] =  useNotificationSettings();
// Current user receives thread notifications on the email channel// { email: { thread: true, ... }, ... }console.log(settings);
// Disabling thread notifications on the email channelupdateSettings({  email: {    thread: false,  },});
```

A user’s initial settings are set in the dashboard, and different kinds should
be enabled there. If no kind is enabled on the current channel, `null` will be
returned. For example, with the email channel:

```ts
const [{ isLoading, error, settings }, updateSettings] =  useNotificationSettings();
// { email: null, ... }console.log(settings);
```

#### [Updating notification settings](/content/docs/api-reference/liveblocks-react\#useNotificationSettings-updating-notification-settings/index.html)

The `updateSettings` function can be used to update the current user’s
notification settings, changing their settings for every room in the project.
Each notification `kind` must first be enabled on your project’s notification
dashboard page before settings can be used.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
// You only need to pass partialsupdateSettings({  email: { thread: true },});
// Enabling a custom notification on the slack channelupdateSettings({  slack: { $myCustomNotification: true },});
// Setting complex settingsupdateSettings({  email: {    thread: true,    textMention: false,    $newDocument: true,  },  slack: {    thread: false,    $fileUpload: false,  },  teams: {    thread: true,  },});
```

Returns

- settingsNotificationSettings \| null

The current notification settings for the user, or null if no settings are
configured.

- updateSettings(settings: Partial<NotificationSettings>) => void

A function to update the notification settings.

- isLoadingboolean

Whether the notification settings are currently being loaded.

- errorError \| null

Any error that occurred while loading the notification settings.

#### [Error handling](/content/docs/api-reference/liveblocks-react\#useNotificationSettings-error-handling/index.html)

Error handling is an important aspect to consider when using the
`useNotificationSettings` hook. The `error` fields provides information about
any error that occurred during the fetch operation.

The following example shows how to display error messages for both initial
loading errors and errors that occur when fetching more inbox notifications.

```tsx
import { useNotificationSettings } from "@liveblocks/react";
const [{ isLoading, error, settings }, updateSettings] =  useNotificationSettings();
if (error) {  return (    <div>      <p>Error loading notification settings: {error.message}</p>    </div>  );}
```

### [useUpdateNotificationSettingsLiveblocksProvider](/content/docs/api-reference/liveblocks-react\#useUpdateNotificationSettings/index.html)

Notification settings is currently in beta.

Returns a function that updates user’s notification settings, which affects
which [notification webhook events](/content/docs/platform/webhooks#NotificationEvent/index.html)
will be sent for the current user. Notification settings are project-based,
which means that `updateSettings` modifies the current user’s settings in every
room. Each notification `kind` must first be enabled on your project’s
notification dashboard page before settings can be used. Useful for creating a
[notification settings panel](/content/docs/guides/how-to-create-a-notification-settings-panel/index.html).

```tsx
import { useUpdateNotificationSettings } from "@liveblocks/react";
const updateSettings = useUpdateNotificationSettings();
// Disabling thread notifications on the email channelupdateSettings({  email: { thread: false },});
```

Works the same as `updateSettings` in
[`useNotificationSettings`](/content/docs/api-reference/liveblocks-react#useNotificationSettings/index.html). You can pass a partial
object, or many settings at once.

Returns

- updateNotificationSettings(settings: Partial<NotificationSettings>) => void

A function that updates the user's notification settings for the current
project.

## [Version History](/content/docs/api-reference/liveblocks-react\#Version-History/index.html)

### [useHistoryVersionsRoomProvider](/content/docs/api-reference/liveblocks-react\#useHistoryVersions/index.html)

Returns the versions of the room. See
[Version History Components](/content/docs/api-reference/liveblocks-react-ui#Version-history-components/index.html)
for more information on how to display versions.

```tsx
import { useHistoryVersions } from "@liveblocks/react";
const { versions, error, isLoading } = useHistoryVersions();
```

Returns

- versionsHistoryVersion\[\]

An array of history versions for the room.

## [Miscellaneous](/content/docs/api-reference/liveblocks-react\#Miscellaneous/index.html)

### [useUserBoth](/content/docs/api-reference/liveblocks-react\#useUser/index.html)

Returns user info from a given user ID. To use `useUser`, you should provide a
resolver function to the [`resolveUsers`](/content/docs/api-reference/liveblocks-client#resolveUsers/index.html) option in [`createClient`](/content/docs/api-reference/liveblocks-client#createClient/index.html) or
[`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html).
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useUser } from "@liveblocks/react";
const { user, error, isLoading } = useUser("user-id");
```

Arguments

- userIdstring

The ID of the user to get information for.

Returns

- userUser \| undefined

The user information, or `undefined` if not found or not yet loaded (in
non-Suspense version).

- isLoadingboolean

Whether the user information is currently being loaded.

- errorError \| undefined

Any error that occurred while loading the user information.

### [useRoomInfoBoth](/content/docs/api-reference/liveblocks-react\#useRoomInfo/index.html)

Returns room info from a given room ID. To use `useRoomInfo`, you should provide
a resolver function to the [`resolveRoomsInfo`](/content/docs/api-reference/liveblocks-client#resolveRoomsInfo/index.html) option in [`createClient`](/content/docs/api-reference/liveblocks-client#createClient/index.html)
or [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html).
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useRoomInfo } from "@liveblocks/react";
const { info, error, isLoading } = useRoomInfo("room-id");
```

Arguments

- roomIdstring

The ID of the room to get information for.

Returns

- infoRoomInfo \| undefined

The room information, or `undefined` if not found or not yet loaded (in
non-Suspense version).

- isLoadingboolean

Whether the room information is currently being loaded.

- errorError \| undefined

Any error that occurred while loading the room information.

### [useGroupInfoBoth](/content/docs/api-reference/liveblocks-react\#useGroupInfo/index.html)

Returns group info from a given group ID. To use `useGroupInfo`, you should
provide a resolver function to the \[`resolveGroupsInfo`\]\[\] option in
[`createClient`](/content/docs/api-reference/liveblocks-client#createClient/index.html) or [`LiveblocksProvider`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html).
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useGroupInfo } from "@liveblocks/react";
const { info, error, isLoading } = useGroupInfo("group-id");
```

Arguments

- groupIdstring

The ID of the group to get information for.

Returns

- infoGroupInfo \| undefined

The group information, or `undefined` if not found or not yet loaded (in
non-Suspense version).

- isLoadingboolean

Whether the group information is currently being loaded.

- errorError \| undefined

Any error that occurred while loading the group information.

### [useUrlMetadataBoth](/content/docs/api-reference/liveblocks-react\#useUrlMetadata/index.html)

Returns metadata for a given URL.
[Suspense](/content/docs/api-reference/liveblocks-react#Suspense-hooks/index.html) and
[regular](/content/docs/api-reference/liveblocks-react#Regular-hooks/index.html) versions of this
hook are available.

```tsx
import { useUrlMetadata } from "@liveblocks/react";
const { metadata, error, isLoading } = useUrlMetadata("https://liveblocks.io");
// metadata.title, metadata.description, metadata.image, metadata.icon, ...
```

Arguments

- urlstring

The URL to get metadata for.

Returns

- metadataUrlMetadata \| undefined

The metadata for the URL, or `undefined` if not yet loaded (in non-Suspense
version).

- isLoadingboolean

Whether the metadata is currently loading.

- errorError \| undefined

Any error that occurred while loading the metadata.

## [TypeScript](/content/docs/api-reference/liveblocks-react\#TypeScript/index.html)

### [Typing your data](/content/docs/api-reference/liveblocks-react\#Typing-your-data/index.html)

It’s possible to have automatic types flow through your application by defining
a global `Liveblocks` interface. We recommend doing this in a
`liveblocks.config.ts` file in the root of your app, so it’s easy to keep track
of your types. Each type (`Presence`, `Storage`, etc.), is optional, but it’s
recommended to make use of them.

liveblocks.config.ts

```ts
declare global {  interface Liveblocks {    // Each user's Presence, for useMyPresence, useOthers, etc.    Presence: {};
    // The Storage tree for the room, for useMutation, useStorage, etc.    Storage: {};
    UserMeta: {      id: string;      // Custom user info set when authenticating with a secret key      info: {};    };
    // Custom events, for useBroadcastEvent, useEventListener    RoomEvent: {};
    // Custom metadata set on threads, for useThreads, useCreateThread, etc.    ThreadMetadata: {};
    // Custom room info set with resolveRoomsInfo, for useRoomInfo    RoomInfo: {};
    // Custom group info set with resolveGroupsInfo, for useGroupInfo    GroupInfo: {};
    // Custom activities data for custom notification kinds    ActivitiesData: {};  }}
// Necessary if you have no imports/exportsexport {};
```

Here are some example values that might be used.

liveblocks.config.ts

```ts
import { LiveList } from "@liveblocks/client";
declare global {  interface Liveblocks {    // Each user's Presence, for useMyPresence, useOthers, etc.    Presence: {      // Example, real-time cursor coordinates      cursor: { x: number; y: number };    };
    // The Storage tree for the room, for useMutation, useStorage, etc.    Storage: {      // Example, a conflict-free list      animals: LiveList<string>;    };
    UserMeta: {      id: string;      // Custom user info set when authenticating with a secret key      info: {        // Example properties, for useSelf, useUser, useOthers, etc.        name: string;        avatar: string;      };    };
    // Custom events, for useBroadcastEvent, useEventListener    // Example has two events, using a union    RoomEvent: { type: "PLAY" } | { type: "REACTION"; emoji: "🔥" };
    // Custom metadata set on threads, for useThreads, useCreateThread, etc.    ThreadMetadata: {      // Example, attaching coordinates to a thread      x: number;      y: number;    };
    // Custom room info set with resolveRoomsInfo, for useRoomInfo    RoomInfo: {      // Example, rooms with a title and url      title: string;      url: string;    };
    // Custom group info set with resolveGroupsInfo, for useGroupInfo    GroupInfo: {      // Example, groups with a name and a badge      name: string;      badge: string;    };
    // Custom activities data for custom notification kinds    ActivitiesData: {      // Example, a custom $alert kind      $alert: {        title: string;        message: string;      };    };  }}
// Necessary if you have no imports/exportsexport {};
```

### [Typing with createRoomContext](/content/docs/api-reference/liveblocks-react\#Typing-with-createRoomContext/index.html)

Before Liveblocks 2.0, it was recommended to create your hooks using
[`createRoomContext`](/content/docs/api-reference/liveblocks-react#createRoomContext/index.html), and manually pass your types to this function. This is
no longer [the recommended method](/content/docs/api-reference/liveblocks-react#Typing-your-data/index.html) for setting up Liveblocks,
but it can still be helpful, for example you can use `createRoomContext`
multiple times to create different room types, each with their own correctly
typed hooks.

liveblocks.config.ts

To upgrade to Liveblocks 2.0 and the new typing system, follow the
[2.0 migration guide](/content/docs/platform/upgrading/2.0).

### [User](/content/docs/api-reference/liveblocks-react\#user-type/index.html)

`User` is a type that’s returned by [`useSelf`](/content/docs/api-reference/liveblocks-react#useSelf/index.html), [`useOthers`](/content/docs/api-reference/liveblocks-react#useOthers/index.html), and other
functions. Some of its values are set when
[typing your room](/content/docs/api-reference/liveblocks-react#Typing-your-data/index.html), here are some example values:

liveblocks.config.ts

```ts
declare global {  interface Liveblocks {    // Each user’s Presence    Presence: {      cursor: { x: number; y: number };    };
    UserMeta: {      id: string;      // Custom user info set when authenticating with a secret key      info: {        name: string;        avatar: string;      };    };  }}
```

```ts
const { room, leave } = client.enterRoom("my-room-id");
// {//   connectionId: 52,//   presence: {//     cursor: { x: 263, y: 786 },//   },//   id: "mislav.abha@example.com",//   info: {//     name: "Mislav Abha",//     avatar: "/mislav.png",//   },//   canWrite: true,//   canComment: true,// }const user = room.getSelf();
```

Properties

- connectionIdnumber

The connection ID of the User. It is unique and increments with every new
connection.

- idUserMeta\["id"\]

The ID of the User that has been set in the authentication endpoint. Useful
to get additional information about the connected user.

- infoUserMeta\["info"\]

Additional user information that has been set in the authentication
endpoint.

- presenceTPresence

The user’s Presence data.

- canWriteboolean

`true` if the user can mutate the Room’s Storage and/or YDoc, `false` if
they can only read but not mutate it. Set via your [room\\
permissions](/content/docs/authentication#Room-permissions/index.html).

- canCommentboolean

`true` if the user can leave a comment in the room, `false` if they can only
read comments but not leave them. Set via your [room\\
permissions](/content/docs/authentication#Room-permissions/index.html).

## [Helpers](/content/docs/api-reference/liveblocks-react\#Helpers/index.html)

### [shallow](/content/docs/api-reference/liveblocks-react\#shallow/index.html)

Compares two values shallowly. This can be used as the second argument to
selector based functions to loosen the equality check:

```tsx
const redShapes = useStorage(  (root) => root.shapes.filter((shape) => shape.color === "red"),  shallow // 👈 here);
```

The default way [selector results](/content/docs/api-reference/liveblocks-react#selectors-return-arbitrary-values/index.html) are
compared is by checking referential equality (`===`). If your selector returns
computed arrays (like in the example above) or objects, this will not work.

By passing `shallow` as the second argument, you can “loosen” this check. This
is because `shallow` will shallowly compare the members of an array (or values
in an object):

```tsx
// Comparing arraysshallow([1, 2, 3], [1, 2, 3]); // true
// Comparison objectsshallow({ a: 1 }, { a: 1 }); // true
```

Please note that this will only do a shallow (one level deep) check. Hence the
name. If you need to do an arbitrarily deep equality check, you’ll have to write
a custom equality function or use a library like Lodash for that.

## [How selectors work](/content/docs/api-reference/liveblocks-react\#selectors/index.html)

The concepts and behaviors described in this section apply to all of our
selector hooks: [`useStorage`](/content/docs/api-reference/liveblocks-react#useStorage/index.html) , [`useSelf`](/content/docs/api-reference/liveblocks-react#useSelf/index.html) , [`useOthers`](/content/docs/api-reference/liveblocks-react#useOthers/index.html) ,
[`useOthersMapped`](/content/docs/api-reference/liveblocks-react#useOthersMapped/index.html), and [`useOther`](/content/docs/api-reference/liveblocks-react#useOther/index.html) (singular).

Component.tsx

```tsx
const child = useStorage((root) => root.child);const nested = useStorage((root) => root.child.nested);const total = useStorage((root) => root.x + root.y);const merged = useStorage((root) => [...root.items, ...root.more], shallow);
```

Examples are illustrated via useStorage

In this section, `useStorage` is used as the canonical example. This is for
illustration purposes only. The described concepts and behaviors apply equally
to the other selector hooks.

In a nutshell, the key behaviors for all selector APIs are:

- They [receive immutable data](/content/docs/api-reference/liveblocks-react#selectors-receive-immutable-data/index.html)
- They [return arbitrary values](/content/docs/api-reference/liveblocks-react#selectors-return-arbitrary-values/index.html)
- They [auto-subscribe to updates](/content/docs/api-reference/liveblocks-react#selectors-subscribe-to-updates/index.html)

Let’s go over these traits and responsibilities in the next few sections.

### [Selectors receive immutable data](/content/docs/api-reference/liveblocks-react\#selectors-receive-immutable-data/index.html)

The received input to all selector functions is a **read-only** and
**immutable** top level context value that differs for each hook:

- `useStorage((root) => ...)` receives the Storage root
- `useSelf((me) => ...)` receives the current user
- `useOthers((others) => ...)` receives a list of other users in the room
- `useOthersMapped((other) => ...)` receives each individual other user in the
room
- `useOther(connectionId, (other) => ...)` receives a specific user in the room

For example, suppose you have set up Storage in the typical way by setting
`initialStorage` in your [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html) to a tree that describes your app’s
data model using `LiveList`, `LiveObject`, and `LiveMap`. The "root" argument
for your selector function, however, will receive **an immutable and read-only**
**representation** of that Storage tree, consisting of "normal" JavaScript
datastructures. This makes consumption much easier.

Component.tsx

```tsx
function Component() {  useStorage((root) => ...);  //          ^^^^  //          Read-only. No mutable Live structures in here.  //  //          {  //            animals: ["🦁", "🦊", "🐵"],  //            mathematician: { firstName: "Ada", lastName: "Lovelace" },  //            fruitsByName: new Map([  //              ["apple", "🍎"],  //              ["banana", "🍌"],  //              ["cherry", "🍒"],  //            ])  //          }  //}
```

Internally, these read-only trees use a technique called **structural sharing**.
This means that between rerenders, if nodes in the tree did not change, they
will **guarantee** to return the same memory instance. Selecting and returning
these nodes directly is therefore safe and considered a good practice, because
they are stable references by design.

### [Selectors return arbitrary values](/content/docs/api-reference/liveblocks-react\#selectors-return-arbitrary-values/index.html)

Component.tsx

```tsx
const animals = useStorage((root) => root.animals);// ["🦁", "🦊", "🐵"]
const ada = useStorage((root) => root.mathematician);// { firstName: "Ada", lastName: "Lovelace" }
const fullname = useStorage(  (root) => `${root.mathematician.firstName} ${root.mathematician.lastName}`);// "Ada Lovelace"
const fruits = useStorage((root) => [...root.fruitsByName.values()], shallow);// ["🍎", "🍌", "🍒"]
```

Selectors you write can return _any_ value. You can use it to “just” select
nodes from the root tree (first two examples above), but you can also return
computed values, like in the last two examples.

#### [Selector functions must return a stable result](/content/docs/api-reference/liveblocks-react\#Selector-functions-must-return-a-stable-result/index.html)

One important rule is that selector functions **must return a stable result** to
be efficient. This means calling the same selector twice with the same argument
should return two results that are _referentially equal_. Special care needs to
be taken when filtering or mapping over arrays, or when returning object
literals, because those operations create new array or object instances on every
call (the reason why is detailed
[in the next section](/content/docs/api-reference/liveblocks-react#selectors-subscribe-to-updates/index.html)).

#### [Examples of stable results](/content/docs/api-reference/liveblocks-react\#Examples-of-stable-results/index.html)

✅ `(root) => root.animals` is stable

Liveblocks guarantees this. All nodes in the Storage tree are stable
references as long as their contents don’t change.

️️⚠️ `(root) => root.animals.map(...)` is not stable

Because `.map()` creates a new array instance every time. You’ll need to use
[`shallow`](/content/docs/api-reference/liveblocks-react#shallow/index.html) here.

✅ `(root) => root.animals.map(...).join(", ")` is stable

Because `.join()` ultimately returns a string and all primitive values are
always stable.

#### [Use a shallow comparison if the result isn’t stable](/content/docs/api-reference/liveblocks-react\#Use-a-shallow-comparison-if-the-result-isn't-stable/index.html)

If your selector function doesn’t return a stable result, it will lead to an
explosion of unnecessary rerenders. In most cases, you can use a [`shallow`](/content/docs/api-reference/liveblocks-react#shallow/index.html)
comparison function to loosen the check:

```tsx
import { shallow } from "@liveblocks/react";
// ❌ Bad - many unnecessary rerendersconst uncheckedItems = useStorage((root) =>  root.todos.filter((item) => !item.done));
// ✅ Greatconst uncheckedItems = useStorage(  (root) => root.todos.filter((item) => !item.done),  shallow // 👈 The fix!);
```

If your selector function constructs complex objects, then a [`shallow`](/content/docs/api-reference/liveblocks-react#shallow/index.html)
comparison may not suffice. In those advanced cases, you can provide your own
custom comparison function, or use `_.isEqual` from Lodash.

### [Selectors auto-subscribe to updates](/content/docs/api-reference/liveblocks-react\#selectors-subscribe-to-updates/index.html)

Selectors effectively automatically subscribe your components to updates to the
selected or computed values. This means that your component will **automatically**
**rerender** when the selected value changes.

Using **multiple selector hooks** within a single React component is perfectly
fine. Each such hook will individually listen for data changes. The component
will rerender if _at least one_ of the hooks requires it. If more than one
selector returns a new value, the component _still only rerenders once_.

Technically, deciding if a rerender is needed works by re-running your selector
function `(root) => root.child` every time something changes inside Liveblocks
storage. Anywhere. That happens often in a busy multiplayer app! The reason why
this is still no problem is that even though `root` will be a different value on
every change, `root.child` will not be if it didn’t change (due to how
Liveblocks internally uses structural sharing).

Only once the returned value is different from the previously returned value,
the component will get rerendered. Otherwise, your component will just remain
idle.

Consider the case:

```tsx
function Component() {  const animals = useStorage((root) => root.animals);}
```

And the following timeline:

- First render, `root.animals` initially is `["🦁", "🦊", "🐵"]`.
- Then, something unrelated elsewhere in Storage is changed. In response to the
change, `root.animals` gets re-evaluated, but it still returns the same
(unchanged) array instance.
- Since the value didn’t change, no rerender is needed.
- Then, someone removes an animal from the list. In response to the change,
`root.animals` gets re-evaluated, and now it returns `["🦁", "🦊"]`.
- Because the previous value and this value are different, the component will
rerender, seeing the updated value.
