useNotifications
Learn how to use the useNotifications hook to fetch and manage notifications in your React application
The useNotifications
hook provides a way to fetch and manage notifications in your application. It includes support for pagination, filtering, and real-time updates.
Hook Parameters
Prop | Type | Default |
---|---|---|
tags? | string[] | - |
read? | boolean | - |
archived? | boolean | - |
limit? | number | - |
onSuccess? | (data: Notification[]) => void | - |
onError? | (error: NovuError) => void | - |
Return Value
Prop | Type | Default |
---|---|---|
notifications? | Notification[] | undefined | - |
error? | NovuError | undefined | - |
isLoading? | boolean | - |
isFetching? | boolean | - |
refetch? | () => Promise<void> | - |
fetchMore? | () => Promise<void> | - |
hasMore? | boolean | - |
readAll? | () => Promise<{ data?: void; error?: NovuError }> | - |
archiveAll? | () => Promise<{ data?: void; error?: NovuError }> | - |
archiveAllRead? | () => Promise<{ data?: void; error?: NovuError }> | - |
Notification Type
The Notification type from @novu/js includes many properties. Here are the most commonly used ones:
Prop | Type | Default |
---|---|---|
id? | string | - |
subject? | string | - |
body? | string | - |
isRead? | boolean | - |
isArchived? | boolean | - |
createdAt? | string | - |
readAt? | string | - |
archivedAt? | string | - |
tags? | string[] | - |
data? | Record<string, unknown> | - |
Example Usage
Here's how to use the useNotifications
hook to fetch and display notifications:
With Filtering
You can filter notifications by various properties:
With Infinite Scroll
You can implement infinite scroll using the fetchMore
function:
Using Notification Actions
The hook provides several actions to manage notifications:
The notifications list is automatically updated in real-time when new notifications arrive or when notifications are marked as read/unread.