Skip to main content

React hooks

The notification-kit/react entry point provides hooks that wrap the core API with React state. React is an optional peer dependency — install react and react-dom to use this entry point.

yarn add react react-dom

useNotifications

import { useNotifications } from 'notification-kit/react';

function Notifications() {
const {
isInitialized, permission, token, error,
init, requestPermission, getToken,
scheduleNotification, showInApp, isPermissionGranted,
} = useNotifications();

useEffect(() => {
init({ provider: 'firebase', config: firebaseConfig });
}, []);

if (error) return <p>Notifications error: {error.message}</p>;

return (
<div>
<button disabled={!isInitialized} onClick={requestPermission}>
{isPermissionGranted ? 'Notifications on' : 'Enable notifications'}
</button>
{token && <code>{token.slice(0, 16)}</code>}
<button onClick={() => showInApp.success('Hi', 'It works')}>
Test in-app
</button>
</div>
);
}

State

FieldType
isInitialized / isInitializingboolean
permissionPermissionStatus | null
tokenstring | null
errorError | null
notificationsNotification[]
pendingNotificationsNotification[]
subscriptionsstring[]
isPermissionGrantedboolean (derived)

Actions

init, destroy, requestPermission, checkPermission, getToken, refreshToken, subscribe, unsubscribe, scheduleNotification, cancelNotification, getPendingNotifications, createChannel, deleteChannel, listChannels, addEventListener, clearNotifications, clearError, refresh, isSupported, and a showInApp object with show / success / error / warning / info.

All returned functions are stable (memoized), and event listeners are cleaned up on unmount.

In-app notification hooks

import {
useInAppNotification,
useInAppNotificationSimple,
useInAppNotificationQueue,
useInAppNotificationPersistence,
} from 'notification-kit/react';
HookUse for
useInAppNotificationFull in-app notification control + state.
useInAppNotificationSimpleMinimal show/dismiss surface.
useInAppNotificationQueueQueued notifications shown one at a time.
useInAppNotificationPersistenceNotifications that persist across renders.

Convenience re-exports

For ergonomics the React entry also re-exports common utilities so a React app can import everything from one place: showInAppNotification, dismissInAppNotification, dismissAllInAppNotifications, getActiveInAppNotifications, configureInAppNotifications, inApp, validate, format, SchedulingUtils, permissions, storage, and platform.

See the React hooks reference for full return types.