The Notifications React hooks can be used to fetch inbox notifications on the client, and change their behavior.
The most important Notifications hook is useInboxNotifications
, which
retrieves every inbox notification for the current user. This can be used to
render a list of notifications, either using the
default components, or with your
own components. Here’s an example of it used with the default
InboxNotification
component.
There are two versions of the useInboxNotifications
hook, the
Suspense version, which we recommend by default, and the
regular version.
The
useUnreadInboxNotificationsCount
hook is useful for creating a badge that displays the unread notifications
count.
To mark all notifications as read for the current user, you can add
useMarkAllInboxNotificationsAsRead
.
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
custom 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.
Comments integrates seamlessly into Notifications, and a number of hooks are provided to modify this behavior.
Threads keep track of unread comments so viewing a thread will also mark its
inbox notification as read, and vice versa. If you use the default
Thread
component, it will
automatically handle marking threads as read when they are viewed and show
unread indicators when there are unread comments in threads you are
participating in.
If you’re building your own custom Thread
component, you can use
useMarkThreadAsRead
and
useThreadSubscription
to replicate or customize this behavior.
By default, you’ll receive inbox notifications for threads you are participating in. This setting can be customized for each user and per room:
"all"
to be notified about everything."replies_and_mentions"
for the default setting."none"
to mute the room.You can use
useRoomNotificationSettings
to build a settings picker in your app and allow users to change their own
notification settings for the current room, or use
updateRoomNotificationSettings
server-side to control them yourself: for example, to automatically make a
document’s author notified about everything in their document.
When using inbox notifications for threads, it can be useful to fetch the actual
thread data, and
useInboxNotificationThread
makes this easy.
There are two different ways to use the notifications and user hooks; with React Suspense, and without it. We recommend using the Suspense versions, as they often result in simpler code.
Using Suspense hooks means that any data retrieved, for example
inboxNotifications
from useInboxNotifications
, 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 the suspense
property in your config file.
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 the root level in your config file.
We use cookies to collect data to improve your experience on our site. Read our Privacy Policy to learn more.