The Comments React hooks can be used to fetch, create, and modify threads on the client.

List a room’s threads

The most important Comments hook is useThreads, which retrieves a list of each thread in the current room. This can be used to render a list of threads, either using the default components, or primitives. Here’s an example of it used with the default Thread component.

There are two versions of the useThreads hook, the Suspense version, which we recommend by default, and the regular version.

Fetch a user’s information

The only information Liveblocks stores about users is their user ID, which is set when authenticating with Liveblocks. With the useUser hook, you can fetch a user’s information from their ID. This is particularly helpful when building primitive components, as this allows you fetch their name, avatar, and any other custom data you’ve set.

The user data retrieved is set within the resolveUsers function in your liveblocks.config.ts file.

There are two versions of the useUser hook, Suspense, which we recommend by default, and regular.

Fetch a group’s information

Similar to users, Liveblocks only stores group IDs for groups. With the useGroupInfo hook, you can fetch a group’s information from their ID. This is particularly helpful when building primitive components, as this allows you to fetch their name, avatar, and any other custom data you’ve set.

The group data retrieved is set within the resolveGroupsInfo function in your liveblocks.config.ts file.

There are two versions of the useGroupInfo hook, Suspense, which we recommend by default, and regular.

Create and modify threads and comments

A number of hooks can be used to mutate comment and thread data, for example:

  • Creating threads
  • Deleting comments
  • Adding emoji reactions

Some of these hooks are quite simple, such as useAddReaction which adds a reaction to a specified comment.

Whereas others, such as useCreateThread, have more complex behaviour and are designed to work alongside primitives.

You can find more information on each mutation hook in our API reference pages:

There’s only one version of these hooks, so it doesn’t make a difference if you export them from suspense in your config file or not.

Hook types

There are two different ways to use the threads and user hooks; with React Suspense, and without it. We recommend using the Suspense versions, as they often result in simpler code.

Suspense hooks

Using Suspense hooks means that any data retrieved, for example threads from useThreads, will never be undefined, and your component will never see an error.

To catch errors and display a loading screen, you can use ErrorBoundary and ClientSideSuspense.

To use Suspense, make sure you’re exporting your hooks from @liveblocks/react/suspense.

Regular hooks

The regular versions of Liveblocks hooks require you to check for error and isLoading properties. You can then handle these states in the same component.

To use the regular hooks, make sure you’re exporting from @liveblocks/react.