DocsReact HooksuseNotifications

useNotifications

The useNotifications hook provides functionality to fetch, manage, and display user notifications. It includes features like automatic grouping by time periods, new notification tracking, and automatic refresh.

Usage

const { notifications, isLoading, isOpen, setIsOpen, newNotifications, refetch } = useNotifications(addressOrName)

Parameters

ParameterTypeDescription
addressOrNameAddress | stringThe Ethereum address or ENS name of the user

Returns

PropertyTypeDescription
notificationsArray<NotificationGroup>Array of notification groups sorted by time periods
isLoadingbooleanWhether notifications are currently loading
isOpenbooleanWhether the notifications dropdown is open
setIsOpen(isOpen: boolean) => voidFunction to control the dropdown open state
newNotificationsnumberCount of new notifications
refetch() => voidFunction to manually refetch notifications
⚠️

Make sure to handle the case when addressOrName is undefined or null, as the hook will not fetch notifications in such cases.

Features

  • Automatic grouping of notifications by time periods:
    • Recent (last hour)
    • 1h, 6h, 12h, 24h
    • 1d, 2d, 3d
    • 1w, 1m
  • Automatic refresh every minute
  • New notification tracking
  • Local storage integration for notification state
  • Support for different notification types:
    • Follow/Unfollow
    • Tag/Untag
    • Block/Unblock
    • Mute/Unmute

Example

import { useNotifications } from 'ethereum-identity-kit'
 
function NotificationsComponent({ address }) {
  const { notifications, isLoading, isOpen, setIsOpen, newNotifications } = useNotifications(address)
 
  return (
    <div>
      <button onClick={() => setIsOpen(!isOpen)}>
        Notifications {newNotifications > 0 && `(${newNotifications})`}
      </button>
 
      {isOpen && (
        <div>
          {isLoading ? (
            <div>Loading...</div>
          ) : (
            notifications?.map((group) => (
              <div key={group.label}>
                <h3>{group.label}</h3>
                {/* Render notifications */}
              </div>
            ))
          )}
        </div>
      )}
    </div>
  )
}

The hook uses React Query under the hood for data fetching and caching. It automatically refetches notifications every minute to keep the data fresh.