useAppNotifications Hook
Replyke’s app notifications feature uses a Redux-powered hook that provides a simple interface for managing user notifications. No provider wrapper is needed - simply use the hook directly in your components.
Basic Usage
import { useAppNotifications } from '@replyke/core';
function NotificationCenter() {
const {
appNotifications,
unreadAppNotificationsCount,
loading,
hasMore,
loadMore,
markNotificationAsRead,
markAllNotificationsAsRead,
resetAppNotifications
} = useAppNotifications();
return (
<div>
<h2>Notifications ({unreadAppNotificationsCount} unread)</h2>
{appNotifications.map(notification => (
<div key={notification.id} onClick={() => markNotificationAsRead(notification.id)}>
{notification.title}
</div>
))}
{hasMore && <button onClick={loadMore}>Load More</button>}
</div>
);
}
Hook Configuration
The useAppNotifications
hook accepts optional configuration props:
interface UseAppNotificationsProps {
limit?: number;
notificationTemplates?: Partial<NotificationTemplates>;
}
Configuration Options:
-
limit: Specifies the number of notifications to fetch per batch. This strikes a balance between efficiency and the number of API calls. The default value is
10
, which is suitable for most use cases. -
notificationTemplates: An object containing templates to customize notification messages. If not provided, default templates are used. This feature is explored in detail in the notification templates section.
Example with Custom Configuration:
const notificationConfig = {
limit: 20,
notificationTemplates: {
entityComment: {
title: "$initiatorName left a comment on your post \"$entityTitle\"",
content: "$commentContent"
},
newFollow: {
title: "$initiatorName started following you!",
content: "Check out their profile: @$initiatorUsername"
}
}
};
function NotificationCenter() {
const notifications = useAppNotifications(notificationConfig);
// ... rest of component
}
Hook Interface
The useAppNotifications hook provides the following data and functions:
interface UseAppNotificationsValues {
appNotifications: UnifiedAppNotification[];
unreadAppNotificationsCount: number;
loading: boolean;
hasMore: boolean;
loadMore: () => void;
markNotificationAsRead: (notificationId: string) => Promise<void>;
markAllNotificationsAsRead: () => Promise<void>;
resetAppNotifications: () => Promise<void>;
}
Available Data:
- appNotifications: An array of all fetched notifications, ordered by creation time.
- unreadAppNotificationsCount: The number of unread notifications.
- loading: A flag indicating if more notifications are currently being fetched.
- hasMore: A flag indicating if there are more notifications to fetch.
Available Functions:
- loadMore: Fetches the next batch of notifications.
- markNotificationAsRead: Expects a notification ID, and marks that notification as “read”.
- markAllNotificationsAsRead: Marks all notifications as “read”.
- resetAppNotifications: Resets and refetches notifications, starting with the first batch.
Ready-to-Use React Component
Replyke also provides a complete, ready-to-use React component that developers can implement in their apps for free. You can see it in action on Replyke landing page. This component works with Replyke out of the box and includes all the UI and functionality needed for a complete notifications experience.
View the React Notifications Component
The component includes:
- Pre-styled notification list with loading states
- Mark as read functionality
- Load more pagination
- Unread count badge
- Customizable styling options
Creating an Engaging Notifications Experience
With the data and functions provided by the useAppNotifications
hook, developers can create a dynamic and engaging notifications experience for users. The Redux-powered architecture ensures optimal performance and state management across your application. Subsequent chapters will delve deeper into customizing notification templates and advanced integration techniques.