UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

33 lines 1.49 kB
import * as React from 'react'; import { useState, useCallback, useMemo } from 'react'; import { NotificationContext } from "./NotificationContext.js"; import { AddNotificationContext } from "./AddNotificationContext.js"; export const NotificationContextProvider = ({ children }) => { const [notifications, setNotifications] = useState([]); const addNotification = useCallback((notification) => { setNotifications(notifications => [...notifications, notification]); }, []); const takeNotification = useCallback(() => { if (notifications.length === 0) return; const [notification, ...rest] = notifications; setNotifications(rest); return notification; }, [notifications]); const resetNotifications = useCallback(() => { setNotifications([]); }, []); const contextValue = useMemo(() => ({ notifications, addNotification, takeNotification, resetNotifications, setNotifications, }), [notifications] // eslint-disable-line react-hooks/exhaustive-deps ); // we separate the addNotification context to avoid rerendering all components // that depend on useNotify when a notification is dispatched return (React.createElement(NotificationContext.Provider, { value: contextValue }, React.createElement(AddNotificationContext.Provider, { value: addNotification }, children))); }; //# sourceMappingURL=NotificationContextProvider.js.map