UNPKG

ra-core

Version:

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

70 lines 3.14 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.NotificationContextProvider = void 0; const React = __importStar(require("react")); const react_1 = require("react"); const NotificationContext_1 = require("./NotificationContext.cjs"); const AddNotificationContext_1 = require("./AddNotificationContext.cjs"); const NotificationContextProvider = ({ children }) => { const [notifications, setNotifications] = (0, react_1.useState)([]); const addNotification = (0, react_1.useCallback)((notification) => { setNotifications(notifications => [...notifications, notification]); }, []); const takeNotification = (0, react_1.useCallback)(() => { if (notifications.length === 0) return; const [notification, ...rest] = notifications; setNotifications(rest); return notification; }, [notifications]); const resetNotifications = (0, react_1.useCallback)(() => { setNotifications([]); }, []); const contextValue = (0, react_1.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_1.NotificationContext.Provider, { value: contextValue }, React.createElement(AddNotificationContext_1.AddNotificationContext.Provider, { value: addNotification }, children))); }; exports.NotificationContextProvider = NotificationContextProvider; //# sourceMappingURL=NotificationContextProvider.js.map