UNPKG

chromogen-zustand

Version:

simple, interaction-driven Jest test generator for Recoil and React Hooks apps

20 lines (19 loc) 925 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useState = void 0; const hooks_core_utils_1 = require("./hooks-core-utils"); const react_1 = require("react"); const hooks_store_1 = require("../utils/hooks-store"); function stateReducer(state, action) { return typeof action === 'function' ? action(state) : action; } const useState = (initialState, id) => { const inspectorStore = (0, react_1.useContext)(hooks_store_1.ObserverContext); const [store, reducerId] = (0, react_1.useMemo)(() => [inspectorStore, id], []); if (!store || !reducerId) { return (0, react_1.useState)(initialState); } const finalInitialState = (0, react_1.useMemo)(() => (typeof initialState === 'function' ? initialState() : initialState), []); return (0, hooks_core_utils_1.useHookedReducer)(stateReducer, finalInitialState, store, reducerId); }; exports.useState = useState;