source: imaps-frontend/node_modules/react-use/esm/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.1 KB
Line 
1import { useState } from 'react';
2import { resolveHookState } from '../misc/hookState';
3import useEffectOnce from '../useEffectOnce';
4import useIsomorphicLayoutEffect from '../useIsomorphicLayoutEffect';
5export function createGlobalState(initialState) {
6 var store = {
7 state: initialState instanceof Function ? initialState() : initialState,
8 setState: function (nextState) {
9 store.state = resolveHookState(nextState, store.state);
10 store.setters.forEach(function (setter) { return setter(store.state); });
11 },
12 setters: [],
13 };
14 return function () {
15 var _a = useState(store.state), globalState = _a[0], stateSetter = _a[1];
16 useEffectOnce(function () { return function () {
17 store.setters = store.setters.filter(function (setter) { return setter !== stateSetter; });
18 }; });
19 useIsomorphicLayoutEffect(function () {
20 if (!store.setters.includes(stateSetter)) {
21 store.setters.push(stateSetter);
22 }
23 });
24 return [globalState, store.setState];
25 };
26}
27export default createGlobalState;
Note: See TracBrowser for help on using the repository browser.