Skip to main content

Create notifications

Oneki.js provides the useNotificationService hook to give access to a central notification service.
The notificationService is a singleton and is created the first time useNotificationService is called.

const notificationService = useNotificationService();


tip

Oneki.js provides a default notificationService. You can redefine it by injecting your own notificationService (see advanced topics)

Parameters

Inputs

There is no input.

Outputs

The output is a NotificationService providing the following methods:

MethodTypeDescription
send(notification: Notification) => voidthe main method to send a notification
remove(id: string) => voidmethod to remove a notification for the Redux state
error(notification: Notification) => voidalias of method 'send' with hardcoded topic=error
success(notification: Notification) => voidalias of method 'send' with hardcoded topic=success
warning(notification: Notification) => voidalias of method 'send' with hardcoded topic=warning
info(notification: Notification) => voidalias of method 'send' with hardcoded topic=info
debug(notification: Notification) => voidalias of method 'send' with hardcoded topic=debug

Notification is an object with the following attributes:

AttributeTypeDescription
topicstringThe topic to be used to send the notification.

Defaults to: default
idstringUnique ID of the notification. This id is used later to remove the notification from the Redux state.

Defaults to: Autogenerated ID
ttlnumberTime to leave of the notification in milliseconds. Set the value to 0 for no expiration

Defaults to: ttl in settings.ts. If not defined in settings.ts, then defaults to 5000 (0 for error topic)
persistbooleanA flag to indicate if the notification should survive after a route change.

Defaults to: persist in settings.ts. If not defined in settings.ts, then defaults to true
payloadobjectYou can put whatever you want in the payload. We recommand an object with at least the field message
Example: { message: "test" }

Defaults to: undefined

Examples

Minimal example

examples/cra-notifications/src/pages/NotificationPage.tsx
loading...