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 | 
|---|---|---|
onError? | (error: NovuError) => void | - | 
onSuccess? | (data: Notification[]) => void | - | 
limit? | number | - | 
archived? | boolean | - | 
read? | boolean | - | 
tags? | string[] | - | 
Return Value
| Prop | Type | Default | 
|---|---|---|
archiveAllRead? | () => Promise<{ data?: void; error?: NovuError }> | - | 
archiveAll? | () => Promise<{ data?: void; error?: NovuError }> | - | 
readAll? | () => Promise<{ data?: void; error?: NovuError }> | - | 
hasMore? | boolean | - | 
fetchMore? | () => Promise<void> | - | 
refetch? | () => Promise<void> | - | 
isFetching? | boolean | - | 
isLoading? | boolean | - | 
error? | NovuError | undefined | - | 
notifications? | Notification[] | undefined | - | 
Notification Type
The Notification type from @novu/js includes many properties. Here are the most commonly used ones:
| Prop | Type | Default | 
|---|---|---|
data? | Record<string, unknown> | - | 
tags? | string[] | - | 
archivedAt? | string | - | 
readAt? | string | - | 
createdAt? | string | - | 
isArchived? | boolean | - | 
isRead? | boolean | - | 
body? | string | - | 
subject? | string | - | 
id? | string | - | 
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.