ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
39 lines (36 loc) • 1.22 kB
text/typescript
import { useCallback, ReactNode } from 'react';
import { useAddNotificationContext } from './useAddNotificationContext';
import { NotificationType, NotificationOptions } from './types';
/**
* Hook for Notification Side Effect
*
* @example
*
* const notify = useNotify();
* // simple message (info level)
* notify('Level complete');
* // specify level
* notify('A problem occurred', { type: 'error' })
* // pass arguments to the translation function
* notify('Deleted %{count} elements', { type: 'info', messageArgs: { smart_count: 23 } })
* // show the action as undoable in the notification
* notify('Post renamed', { type: 'info', undoable: true })
*/
export const useNotify = () => {
const addNotification = useAddNotificationContext();
return useCallback(
(
message: string | ReactNode,
options: NotificationOptions & { type?: NotificationType } = {}
) => {
const { type: messageType = 'info', ...notificationOptions } =
options;
addNotification({
message,
type: messageType,
notificationOptions,
});
},
[addNotification]
);
};