react-hooks-global-state
Version:
Simple global state for React with Hooks API without Context API
3 lines (2 loc) • 1.91 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("zustand"),require("zustand/middleware")):"function"==typeof define&&define.amd?define(["exports","react","zustand","zustand/middleware"],e):e((t||self).reactHooksGlobalState={},t.React,t.zustand,t.middleware)}(this,function(t,e,n,r){function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var u=/*#__PURE__*/o(n),a=function(t,e){if(!t.includes(e))throw new Error("'"+e+"' not found. It must be provided in initialState as a property key.")};t.createGlobalState=function(t){var n=u.default(function(){return t}),r=Object.keys(t),o=function(t,e){"production"!==process.env.NODE_ENV&&a(r,t),n.setState(function(n){var r,o;return(r={})[t]="function"==typeof(o=e)?o(n[t]):o,r})};return{useGlobalState:function(t){"production"!==process.env.NODE_ENV&&a(r,t);var u=e.useCallback(function(e){return e[t]},[t]);return[n(u),e.useCallback(function(e){return o(t,e)},[t])]},getGlobalState:function(t){return"production"!==process.env.NODE_ENV&&a(r,t),n.getState()[t]},setGlobalState:o,subscribe:function(t,e){n.subscribe(function(n,r){n[t]!==r[t]&&e(n[t])})}}},t.createStore=function t(n,o,a){if(void 0===o&&(o=n(void 0,{type:void 0})),a)return a(t)(n,o);var i=u.default(r.redux(n,o)),c=Object.keys(o),s=function(t){"production"!==process.env.NODE_ENV&&function(t,e){if(!t.includes(e))throw new Error("'"+e+"' not found. It must be provided in initialState as a property key.")}(c,t);var n=e.useCallback(function(e){return e[t]},[t]);return i(n)};return{useStoreState:s,useGlobalState:function(t){return"production"!==process.env.NODE_ENV&&console.warn("[DEPRECATED] useStoreState instead"),[s(t),e.useCallback(function(e){i.setState(function(n){var r,o;return(r={})[t]="function"==typeof(o=e)?o(n[t]):o,r})},[t])]},getState:i.getState,dispatch:i.dispatch}}});
//# sourceMappingURL=index.umd.js.map