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 | - |
createdLte? | number | - |
createdGte? | number | - |
severity? | SeverityLevelEnum | SeverityLevelEnum[] | - |
seen? | boolean | - |
snoozed? | boolean | - |
archived? | boolean | - |
read? | boolean | - |
data? | Record<string, unknown> | - |
tags? | string[] | - |
Return Value
| Prop | Type | Default |
|---|---|---|
fetchMore? | () => Promise<void> | - |
refetch? | () => Promise<void> | - |
archiveAllRead? | () => Promise<{ data?: void | undefined; error?: NovuError | undefined; }> | - |
archiveAll? | () => Promise<{ data?: void | undefined; error?: NovuError | undefined; }> | - |
seenAll? | () => Promise<{ data?: void | undefined; error?: NovuError | undefined; }> | - |
readAll? | () => Promise<{ data?: void | undefined; error?: NovuError | undefined; }> | - |
hasMore? | boolean | - |
isFetching? | boolean | - |
isLoading? | boolean | - |
error? | NovuError | - |
notifications? | Notification[] | - |
Notification Type
The Notification type from @novu/react includes many properties. Here are the most commonly used ones:
| Prop | Type | Default |
|---|---|---|
off? | <Key extends EventNames>(eventName: Key, listener: EventHandler<Events[Key]>) => void | - |
on? | <Key extends EventNames>(eventName: Key, listener: EventHandler<Events[Key]>) => () => void | - |
revertSecondary? | () => Result<Notification, NovuError> | - |
revertPrimary? | () => Result<Notification, NovuError> | - |
completeSecondary? | () => Result<Notification, NovuError> | - |
completePrimary? | () => Result<Notification, NovuError> | - |
unsnooze? | () => Result<Notification, NovuError> | - |
snooze? | (snoozeUntil: string) => Result<Notification, NovuError> | - |
delete? | () => Result<void, NovuError> | - |
unarchive? | () => Result<Notification, NovuError> | - |
archive? | () => Result<Notification, NovuError> | - |
seen? | () => Result<Notification, NovuError> | - |
unread? | () => Result<Notification, NovuError> | - |
read? | () => Result<Notification, NovuError> | - |
severity? | SeverityLevelEnum | - |
workflow? | Workflow | - |
data? | NotificationData | - |
redirect? | Redirect | undefined | - |
tags? | string[] | undefined | - |
channelType? | ChannelType | - |
secondaryAction? | Action | - |
primaryAction? | Action | - |
avatar? | string | - |
archivedAt? | string | null | - |
firstSeenAt? | string | null | - |
readAt? | string | null | - |
createdAt? | string | - |
deliveredAt? | string[] | - |
snoozedUntil? | string | null | - |
isSnoozed? | boolean | - |
isArchived? | boolean | - |
isSeen? | boolean | - |
isRead? | boolean | - |
to? | Subscriber | - |
body? | string | - |
subject? | string | - |
transactionId? | 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:
read- Filter notifications by read statusseen- Filter notifications by seen statustags- Filter notifications by tags. If multiple tags are provided, Novu evaluates them usingORlogic, meaning a notification is included if it contains at least one of the specified tags.data- Filter notifications by data attributes (key-value pairs).severity- Filter notifications by severitylimit- Fetch notifications per pagecreatedGte- Fetch notifications created after the given date (in milliseconds)createdLte- Fetch notifications created before the given date (in milliseconds)
With Infinite Scroll
You can implement infinite scroll using the fetchMore function:
Using Notification Actions
The hook provides several actions to manage notifications:
Real-time notifications updates
The notifications list is automatically updated in real-time when new notifications arrive or when notifications state (read, seen, archived, snoozed) changes.