useGMFYNotifications
import { useGmfyNotifications } from 'gmfy-sdk'
Motivation
If you need to draw your own UI for the list of notifications or just process the data, you can use the useGmfyNotifications
hook.
The hook allows you to get a list of user notifications with userId
.
In case updateInterval
was passed in notification provider component config, it will return new values every updateInterval
ms.
Returns a list of notifications and the current request status
caution
Important! The hook must be wrapped in a provider component GMFYNotifications
Typing
type Notification = {
userId:string
clientId:string
message:string;
};
type Notifications = Array<Notification> | null;
type Status = 'initial' | | 'done' | 'fail'
type UpdateValue=
| Notifications
| ((prevState: Notifications) => Notifications)
useGMFYBadges(): {
notifications: Notifications;
status: Status;
setNotifications: (updateValue: UpdateValue) => void;
}
Notification
userId
- ID of the user the notification belongs toclientId
- ID of the client the notification belongs tomessage
- notification message
Status
initial
- initial status, the request has not yet been sentdone
- the request was successfulfail
- an error occurred while executing the request
UpdateValue
Allows you to set a new value for the list by removing viewed notifications. The value will be in the form:
- Either a new list value
- Or a function that takes the previous value and returns the new one
Usage
import { GMFYProvider, GMFYNotifications, useGMFYBadges } from 'gmfy-sdk';
const Notifications = () => {
const { notifications, status } = GMFYNotifications();
if (status !== 'done') {
return<Preloader/>;
}
return notifications.map((notification) => <YourNotificationComponent {...notification} />);
};
const App = () => (
<GMFYProvider config={config}>
<GMFYNotifications updateInterval={5000}>
<Notifications/>
</GMFYNotifications>
</GMFYProvider>
)