docs/platform/sdks/react/hooks/subscription.mdx
The @novu/react package provides a set of Subscription hooks for managing topic subscriptions without using the prebuilt Subscription components.
These hooks let you build fully custom experiences by working directly with the subscription data model.
<Note> If you prefer to use the default Subscription components, refer to the [Subscription components documentation](/platform/subscription/quickstart). </Note>useSubscriptionsFetches all subscriptions associated with a specific topic for the current subscriber.
| Property | Type | Description |
|---|---|---|
topicKey | string | The unique key of the topic to fetch subscriptions for. |
onSuccess | (data: TopicSubscription[]) => void | Callback function called when subscriptions are successfully fetched |
onError | (error: NovuError) => void | Callback function called when an error occurs |
| Property | Type | Description |
|---|---|---|
subscriptions | TopicSubscription[] | Array of subscription objects for the topic (default: []) |
error | `NovuError | undefined` |
isLoading | boolean | True during the initial load |
isFetching | boolean | True while any request is in flight (initial load or refetch) |
refetch | () => Promise<void> | Function to manually trigger a refetch of the list |
This example fetches all subscriptions for a given topic and exposes them for rendering once loading completes.
import { useSubscriptions } from "@novu/react";
const { subscriptions, isLoading } = useSubscriptions({
topicKey: "project-123",
});
useSubscriptionFetches a specific subscription instance.
| Property | Type | Description |
|---|---|---|
topicKey | string | The unique key of the topic. |
identifier | string | The unique identifier of the specific subscription instance. |
onSuccess | `(data: TopicSubscription | null) => void` |
onError | (error: NovuError) => void | Callback function called when an error occurs |
| Property | Type | Description |
|---|---|---|
subscription | `TopicSubscription | null` |
error | `NovuError | undefined` |
isLoading | boolean | True during the initial load |
refetch | () => Promise<void> | Function to manually trigger a refetch |
This example shows how to retrieve a specific subscription by its identifier to access its preferences.
import { useSubscription } from "@novu/react";
const { subscription, isLoading } = useSubscription({
topicKey: "project-123",
identifier: "user-preference-1",
});
useCreateSubscriptionCreates a new subscription to a topic for the current subscriber.
| Property | Type | Description |
|---|---|---|
create | (args: CreateSubscriptionArgs) => Promise<{ data?: TopicSubscription; error?: NovuError }> | Function to create a new subscription. |
isCreating | boolean | True while the create request is in progress |
error | `NovuError | undefined` |
This example demonstrates how to trigger the creation of a new subscription for a topic.
import { useCreateSubscription } from "@novu/react";
const { create, isCreating } = useCreateSubscription();
await create({
topicKey: "project-123",
identifier: "project-updates",
});
useUpdateSubscriptionUpdates an existing subscription's properties or preferences list.
| Property | Type | Description |
|---|---|---|
update | (args: UpdateSubscriptionArgs) => Promise<{ data?: TopicSubscription; error?: NovuError }> | Function to update an existing subscription. |
isUpdating | boolean | True while the update request is in progress |
error | `NovuError | undefined` |
This example shows how to update the preferences of an existing subscription, such as toggling a specific workflow.
import { useUpdateSubscription } from "@novu/react";
const { update, isUpdating } = useUpdateSubscription();
const handleToggle = async () => {
await update({
topicKey: "project-123",
subscriptionId: "sub-id-123",
preferences: [{ workflowId: "workflow-one", enabled: false }]
});
};
useRemoveSubscriptionUnsubscribes the current user from a topic by removing the subscription instance.
| Property | Type | Description |
|---|---|---|
remove | (args: DeleteSubscriptionArgs) => Promise<{ data?: void; error?: NovuError }> | Function to remove a subscription. |
isRemoving | boolean | True while the delete request is in progress |
error | `NovuError | undefined` |
This example renders an unsubscribe button that removes the subscription when clicked.
import { useRemoveSubscription } from "@novu/react";
function UnsubscribeButton({ subscriptionId }) {
const { remove, isRemoving } = useRemoveSubscription();
return (
<button
onClick={() =>
remove({
subscriptionId,
topicKey: "project-123",
})
}
disabled={isRemoving}
>
{isRemoving ? "Removing..." : "Unsubscribe"}
</button>
);
}