usePreferences
Learn how to use the usePreferences hook to manage notification preferences in your React application
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
Prop | Type | Default |
---|---|---|
filter? | { tags?: string[] } | - |
onSuccess? | (data: Preference[]) => void | - |
onError? | (error: NovuError) => void | - |
Return Value
Prop | Type | Default |
---|---|---|
preferences? | Preference[] | undefined | - |
error? | NovuError | undefined | - |
isLoading? | boolean | true |
isFetching? | boolean | true |
refetch? | () => Promise<void> | - |
Preference Type
The Preference type from @novu/js includes these properties:
Prop | Type | Default |
---|---|---|
level? | PreferenceLevel | - |
enabled? | boolean | - |
channels? | { email?: boolean; sms?: boolean; in_app?: boolean; chat?: boolean; push?: boolean; } | - |
workflow? | { id: string; name: string; critical: boolean; } | - |
Example Usage
Here's how to use the usePreferences
hook to display and manage notification preferences:
With Filtering
You can filter preferences by tags:
With Channel Groups
You can organize preferences by channel type:
Changes to preferences are automatically synchronized with the server and will affect future notifications immediately.