The usePreferences hook provides a way to fetch and manage notification preferences for the current subscriber. This includes both global preferences and workflow-specific preferences.
Hook parameters#
| Property | Type | Description |
|---|---|---|
filter | { tags?: string[]; severity?: SeverityLevelEnum[] } | Filter preferences by tags and severity |
onSuccess | (data: Preference[]) => void | Callback function called when preferences are successfully fetched |
onError | (error: NovuError) => void | Callback function called when an error occurs |
Return value#
| Property | Type | Description |
|---|---|---|
preferences | `Preference[] | undefined` |
error | `NovuError | undefined` |
isLoading | boolean | True during the initial load, false otherwise (default: true) |
isFetching | boolean | True during any loading state (initial or refetch), false otherwise (default: true) |
refetch | () => Promise<void> | Function to manually trigger a refetch of the preferences |
Preference type#
The Preference type from @novu/react includes these properties:
| Property | Type | Description |
|---|---|---|
level | PreferenceLevel | Level of the preference (global or template) |
enabled | boolean | Whether notifications are enabled for this preference |
channels | { email?: boolean; sms?: boolean; in_app?: boolean; chat?: boolean; push?: boolean; } | Channel-specific preferences |
workflow | { id: string; name: string; enabled: boolean; } | Workflow-specific preference details |
Example usage#
Here’s how to use the usePreferences hook to display and manage notification preferences:
With filtering#
You can filter preferences by tags or severity:
With channel groups#
You can organize preferences by channel type:
Note
Changes to preferences are automatically synchronized with the server and will affect future notifications immediately.