Skip to main content

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#

PropertyTypeDescription
filter{ tags?: string[]; severity?: SeverityLevelEnum[] }Filter preferences by tags and severity
onSuccess(data: Preference[]) => voidCallback function called when preferences are successfully fetched
onError(error: NovuError) => voidCallback function called when an error occurs

Return value#

PropertyTypeDescription
preferences`Preference[]undefined`
error`NovuErrorundefined`
isLoadingbooleanTrue during the initial load, false otherwise (default: true)
isFetchingbooleanTrue 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:

PropertyTypeDescription
levelPreferenceLevelLevel of the preference (global or template)
enabledbooleanWhether 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.