The usePreferences hook is used to retrieve the subscriber preferences. The first item in the list will represent the subscriber global preferences, while the remaining items will correspond to the preferences for each workflow.

The hook returns both critical and non-critical preferences. Critical preferences are those that are required for the subscriber to receive notifications, while non-critical preferences are those that are optional and could be controlled by the subscriber. You can learn more about the preferences here.

By default the preferences are cached, but you can use the refetch function to fetch the latest preferences.

usePreferences props

onSuccess
Function

Callback function that will be called when the subscriber preferences are successfully fetched. In the callback argument, you will receive the fetched preferences.

onError
Function

Callback function that will be called when there is an error fetching the subscriber preferences. In the callback argument, you will receive the error object.

usePreferences returns

preferences
Preference[] | undefined

An array of subscriber preferences fetched by the hook. If there are no preferences fetched yet, it will be undefined.

isLoading
boolean

A boolean value indicating the first fetch for preferences is in-flight.

isFetching
boolean

A boolean value indicating whether the hook is fetching more preferences.

error
NovuError | undefined

An error object that will be populated if there is an error fetching the preferences.

refetch
Function

A function that can be called to refetch the preferences.

Example usage

import { usePreferences } from '@novu/react/hooks';

const PreferencesList = () => {
  const { preferences, isLoading, error, refetch } = usePreferences();

  return (
    <Show when={!isLoading} fallback={<PreferencesListSkeleton />}>
      {preferences?.map((preference) => {
        return <PreferenceItem key={preference.id} preference={preference} />;
      })}
    </Show>
  );
};