react-hooks-global-state
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -5,2 +5,6 @@ # Change Log | ||
## [2.1.0] - 2022-12-04 | ||
### Added | ||
- expose "subscribe" function for "createGlobalState" #85 | ||
## [2.0.0] - 2022-08-05 | ||
@@ -7,0 +11,0 @@ ### Changed from 2.0.0-rc.0 |
@@ -1,2 +0,2 @@ | ||
import{useCallback as t}from"react";import e from"zustand";import{redux as o}from"zustand/middleware";const r=(t,e)=>{if(!t.includes(e))throw new Error(`'${e}' not found. It must be provided in initialState as a property key.`)},n=o=>{const n=e(()=>o),s=Object.keys(o),a=(t,e)=>{"production"!==process.env.NODE_ENV&&r(s,t),n.setState(o=>{return{[t]:(r=o[t],n=e,"function"==typeof n?n(r):n)};var r,n})};return{useGlobalState:e=>{"production"!==process.env.NODE_ENV&&r(s,e);const o=t(t=>t[e],[e]);return[n(o),t(t=>a(e,t),[e])]},getGlobalState:t=>("production"!==process.env.NODE_ENV&&r(s,t),n.getState()[t]),setGlobalState:a}},s=(r,n=r(void 0,{type:void 0}),a)=>{if(a)return a(s)(r,n);const i=e(o(r,n)),u=Object.keys(n),c=e=>{"production"!==process.env.NODE_ENV&&((t,e)=>{if(!t.includes(e))throw new Error(`'${e}' not found. It must be provided in initialState as a property key.`)})(u,e);const o=t(t=>t[e],[e]);return i(o)};return{useStoreState:c,useGlobalState:e=>("production"!==process.env.NODE_ENV&&console.warn("[DEPRECATED] useStoreState instead"),[c(e),t(t=>{i.setState(o=>{return{[e]:(r=o[e],n=t,"function"==typeof n?n(r):n)};var r,n})},[e])]),getState:i.getState,dispatch:i.dispatch}};export{n as createGlobalState,s as createStore}; | ||
import{useCallback as t}from"react";import e from"zustand";import{redux as r}from"zustand/middleware";const o=(t,e)=>{if(!t.includes(e))throw new Error(`'${e}' not found. It must be provided in initialState as a property key.`)},n=r=>{const n=e(()=>r),s=Object.keys(r),a=(t,e)=>{"production"!==process.env.NODE_ENV&&o(s,t),n.setState(r=>{return{[t]:(o=r[t],n=e,"function"==typeof n?n(o):n)};var o,n})};return{useGlobalState:e=>{"production"!==process.env.NODE_ENV&&o(s,e);const r=t(t=>t[e],[e]);return[n(r),t(t=>a(e,t),[e])]},getGlobalState:t=>("production"!==process.env.NODE_ENV&&o(s,t),n.getState()[t]),setGlobalState:a,subscribe:(t,e)=>{n.subscribe((r,o)=>{r[t]!==o[t]&&e(r[t])})}}},s=(o,n=o(void 0,{type:void 0}),a)=>{if(a)return a(s)(o,n);const i=e(r(o,n)),u=Object.keys(n),c=e=>{"production"!==process.env.NODE_ENV&&((t,e)=>{if(!t.includes(e))throw new Error(`'${e}' not found. It must be provided in initialState as a property key.`)})(u,e);const r=t(t=>t[e],[e]);return i(r)};return{useStoreState:c,useGlobalState:e=>("production"!==process.env.NODE_ENV&&console.warn("[DEPRECATED] useStoreState instead"),[c(e),t(t=>{i.setState(r=>{return{[e]:(o=r[e],n=t,"function"==typeof n?n(o):n)};var o,n})},[e])]),getState:i.getState,dispatch:i.dispatch}};export{n as createGlobalState,s as createStore}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
!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}},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}}}); | ||
!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 |
@@ -9,2 +9,3 @@ import { SetStateAction } from 'react'; | ||
* - `setGlobalState`: a function to set a global state by key outside React | ||
* - `subscribe`: a function that subscribes to state changes | ||
* | ||
@@ -25,2 +26,3 @@ * @example | ||
setGlobalState: <StateKey_2 extends keyof State>(stateKey: StateKey_2, update: SetStateAction<State[StateKey_2]>) => void; | ||
subscribe: <StateKey_3 extends keyof State>(stateKey: StateKey_3, listener: (value: State[StateKey_3]) => void) => void; | ||
}; |
{ | ||
"name": "react-hooks-global-state", | ||
"description": "Simple global state for React with Hooks API without Context API", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "Daishi Kato", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -108,2 +108,3 @@ # react-hooks-global-state | ||
* `setGlobalState`: a function to set a global state by key outside React | ||
* `subscribe`: a function that subscribes to state changes | ||
@@ -110,0 +111,0 @@ #### Parameters |
@@ -24,2 +24,3 @@ import { SetStateAction, useCallback } from 'react'; | ||
* - `setGlobalState`: a function to set a global state by key outside React | ||
* - `subscribe`: a function that subscribes to state changes | ||
* | ||
@@ -74,2 +75,13 @@ * @example | ||
const subscribe = <StateKey extends StateKeys>( | ||
stateKey: StateKey, | ||
listener: (value: State[StateKey]) => void, | ||
) => { | ||
useStore.subscribe((state, prevState) => { | ||
if (state[stateKey] !== prevState[stateKey]) { | ||
listener(state[stateKey]); | ||
} | ||
}); | ||
}; | ||
return { | ||
@@ -79,3 +91,4 @@ useGlobalState, | ||
setGlobalState, | ||
subscribe, | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55807
271
212