source: imaps-frontend/node_modules/react-use/lib/factory/createGlobalState.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.4 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createGlobalState = void 0;
4var tslib_1 = require("tslib");
5var react_1 = require("react");
6var hookState_1 = require("../misc/hookState");
7var useEffectOnce_1 = tslib_1.__importDefault(require("../useEffectOnce"));
8var useIsomorphicLayoutEffect_1 = tslib_1.__importDefault(require("../useIsomorphicLayoutEffect"));
9function createGlobalState(initialState) {
10 var store = {
11 state: initialState instanceof Function ? initialState() : initialState,
12 setState: function (nextState) {
13 store.state = hookState_1.resolveHookState(nextState, store.state);
14 store.setters.forEach(function (setter) { return setter(store.state); });
15 },
16 setters: [],
17 };
18 return function () {
19 var _a = react_1.useState(store.state), globalState = _a[0], stateSetter = _a[1];
20 useEffectOnce_1.default(function () { return function () {
21 store.setters = store.setters.filter(function (setter) { return setter !== stateSetter; });
22 }; });
23 useIsomorphicLayoutEffect_1.default(function () {
24 if (!store.setters.includes(stateSetter)) {
25 store.setters.push(stateSetter);
26 }
27 });
28 return [globalState, store.setState];
29 };
30}
31exports.createGlobalState = createGlobalState;
32exports.default = createGlobalState;
Note: See TracBrowser for help on using the repository browser.