Skip to main content

useNotifications

The useNotifications hook provides access to user notifications with support for pagination, marking as read/unread, starring, and archiving.

Return Value

loading
boolean
Whether notifications are loading
notifications
Notification[]
List of notifications
hasMore
boolean
Whether there are more notifications to load
error
Error | null
Any error that occurred
refetch
loadMore
markAsRead
markAsUnread
markAllAsRead
archiveAllRead
archiveNotification
starNotification
import { useNotifications } from "@wacht/react-router";

function NotificationsPage() {
  const { notifications, loading, markAsRead, markAllAsRead, loadMore } = useNotifications();

  return (
    <div>
      <h1>Notifications</h1>
      <button onClick={() => markAllAsRead()}>Mark All Read</button>
      <ul>
        {notifications.map((notif) => (
          <li key={notif.id}>
            <span>{notif.title}</span>
            <button onClick={() => markAsRead(notif.id)}>
              Mark Read
            </button>
          </li>
        ))}
      </ul>
      <button onClick={() => loadMore()} disabled={!hasMore}>
        Load More
      </button>
    </div>
  );
}

Options

limit
number
Maximum number of notifications to fetch
scope
string
Filter notifications by scope
is_read
boolean
Filter by read status
is_archived
boolean
Filter by archived status
is_starred
boolean
Filter by starred status
severity
string
Filter by severity level
onNotification

Data Structures

Examples

Basic Notifications List

import { useNotifications } from "@wacht/react-router";

function NotificationsList() {
  const { notifications, loading, markAsRead } = useNotifications();

  if (loading) return <div>Loading...</div>;

  return (
    <ul>
      {notifications.map((notif) => (
        <li key={notif.id} onClick={() => markAsRead(notif.id)}>
          {notif.title}: {notif.body}
        </li>
      ))}
    </ul>
  );
}

With Options

import { useNotifications } from "@wacht/react-router";

function UnreadOnly() {
  const { notifications } = useNotifications({
    limit: 20,
    is_read: false,
  });

  return <div>{/* Render notifications */}</div>;
}

Mark All as Read

import { useNotifications } from "@wacht/react-router";

function MarkAllButton() {
  const { markAllAsRead } = useNotifications();

  return (
    <button onClick={() => markAllAsRead()}>
      Mark All as Read
    </button>
  );
}

With Real-time Updates

import { useNotifications } from "@wacht/react-router";

function NotificationBell() {
  const { notifications, onNotification } = useNotifications({
    onNotification: (notification) => {
      toast.info(`New: ${notification.title}`);
    },
  });

  return (
    <Bell count={notifications.length} />
  );
}