react-forceupdate
Advanced tools
+4
-4
@@ -1,4 +0,4 @@ | ||
| declare type UseForceUpdatePropsItem = string | number | undefined; | ||
| declare type UseForceUpdatePropsItem = string | undefined; | ||
| export declare type UseForceUpdateProps = UseForceUpdatePropsItem | Array<UseForceUpdatePropsItem>; | ||
| declare type Input = string | number | undefined; | ||
| declare type Input = string | undefined; | ||
| declare type RunForceUpdatePropsItem = { | ||
@@ -8,6 +8,6 @@ type?: Input; | ||
| }; | ||
| export declare type RunForceUpdateProps = RunForceUpdatePropsItem | Array<RunForceUpdatePropsItem>; | ||
| export declare type RunForceUpdateProps = Input | RunForceUpdatePropsItem; | ||
| export declare type UseForceUpdateState = { | ||
| count: number; | ||
| event?: string; | ||
| type?: string; | ||
| payload?: any; | ||
@@ -14,0 +14,0 @@ }; |
@@ -16,3 +16,3 @@ 'use strict'; | ||
| function add(typeItem) { | ||
| if (typeItem === '*') result.push('*');else if (typeof typeItem === 'undefined') result.push('update');else if (typeItem === '') result.push('update');else result.push('update_' + typeItem); | ||
| if (typeItem === '*') result.push('*');else if (typeof typeItem === 'undefined') result.push('update_default');else result.push('update_' + typeItem); | ||
| } | ||
@@ -32,3 +32,4 @@ | ||
| var getEmitKey = function getEmitKey(type) { | ||
| return typeof type !== 'undefined' ? 'update_' + type : 'update'; | ||
| if (typeof type === 'undefined') return 'update_default'; | ||
| return 'update_' + type; | ||
| }; | ||
@@ -40,10 +41,8 @@ | ||
| function runForceUpdate(props) { | ||
| if (Array.isArray(props)) { | ||
| props.forEach(function (p) { | ||
| var type = getEmitKey(p === null || p === void 0 ? void 0 : p.type); | ||
| emitter.emit(type, p === null || p === void 0 ? void 0 : p.payload); | ||
| }); | ||
| if (typeof props === 'undefined' || typeof props === 'string') { | ||
| emitter.emit(getEmitKey(props)); | ||
| } else { | ||
| var type = getEmitKey(props === null || props === void 0 ? void 0 : props.type); | ||
| emitter.emit(type, props === null || props === void 0 ? void 0 : props.payload); | ||
| var typedProps = props; | ||
| var payload = typedProps === null || typedProps === void 0 ? void 0 : typedProps.payload; | ||
| typeof payload === 'undefined' ? emitter.emit(getEmitKey(typedProps.type)) : emitter.emit(getEmitKey(typedProps.type), payload); | ||
| } | ||
@@ -71,16 +70,19 @@ } | ||
| function updateState(mittType, mittEvt) { | ||
| // Cumbersome parsing due to the order of args in mitt depending on whether type is '*' or not. | ||
| // Order of args in mitt depending on whether type is '*' or not. | ||
| // Either it is (mittType, mittEvt) or (mittEvt). | ||
| // https://github.com/developit/mitt/blob/master/src/index.js | ||
| var event = mittEvt ? mittType : undefined; | ||
| var payload = mittEvt ? mittEvt : mittType; | ||
| var type = mittEvt ? mittType : undefined; | ||
| var payload = mittEvt ? mittEvt : mittType; // this triggers re-render | ||
| setState(function (prevState) { | ||
| var count = (prevState.count || 0) + 1; | ||
| var result = { | ||
| event: event, | ||
| type: type, | ||
| payload: payload, | ||
| count: count | ||
| }; | ||
| if (type === undefined) delete result.type; | ||
| if (payload === undefined) delete result.payload; | ||
| return result; | ||
| }); // this triggers re-render | ||
| }); | ||
| } | ||
@@ -87,0 +89,0 @@ |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"react-forceupdate.cjs.development.js","sources":["../src/index.tsx"],"sourcesContent":["import React from 'react'\nimport mitt from 'mitt'\n\n// Please do not use types off of a default export module or else Storybook Docs will suffer.\n// see: https://github.com/storybookjs/storybook/issues/9556\n\ntype UseForceUpdatePropsItem = string | number | undefined\nexport type UseForceUpdateProps =\n | UseForceUpdatePropsItem\n | Array<UseForceUpdatePropsItem>\n\ntype Input = string | number | undefined\n\ntype RunForceUpdatePropsItem = {\n type?: Input\n payload?: any\n}\n\nexport type RunForceUpdateProps =\n | RunForceUpdatePropsItem\n | Array<RunForceUpdatePropsItem>\n\nexport type UseForceUpdateState = {\n count: number\n event?: string\n payload?: any\n}\n\nlet emitter: mitt.Emitter = mitt()\n\nlet getOnKeys = (type: UseForceUpdateProps): Array<string> => {\n let result = []\n\n function add(typeItem) {\n if (typeItem === '*') result.push('*')\n else if (typeof typeItem === 'undefined') result.push('update')\n else if (typeItem === '') result.push('update')\n else result.push('update_' + typeItem)\n }\n\n if (Array.isArray(type)) {\n type.forEach(typeItem => {\n add(typeItem)\n })\n } else {\n add(type)\n }\n\n return result\n}\nlet getEmitKey = (type: Input): string =>\n typeof type !== 'undefined' ? 'update_' + type : 'update'\n\nlet initialState: UseForceUpdateState = { count: 0 }\n\nexport function runForceUpdate(props?: RunForceUpdateProps) {\n if (Array.isArray(props)) {\n ;(props as Array<RunForceUpdatePropsItem>).forEach(p => {\n let type = getEmitKey(p?.type)\n emitter.emit(type, p?.payload)\n })\n } else {\n let type = getEmitKey(props?.type)\n\n emitter.emit(type, props?.payload)\n }\n}\n\nexport function useForceUpdate(type?: Input): UseForceUpdateState {\n const [state, setState] = React.useState(initialState)\n\n let keys = React.useMemo(() => {\n return getOnKeys(type)\n }, [])\n\n React.useEffect(() => {\n keys.forEach(key => {\n emitter.on(key, updateState)\n })\n\n return () => {\n keys.forEach(key => {\n emitter.off(key, updateState)\n })\n }\n })\n\n function updateState(mittType: any, mittEvt?: any) {\n // Cumbersome parsing due to the order of args in mitt depending on whether type is '*' or not.\n // Either it is (mittType, mittEvt) or (mittEvt).\n // https://github.com/developit/mitt/blob/master/src/index.js\n\n let event = mittEvt ? mittType : undefined\n let payload = mittEvt ? mittEvt : mittType\n let data = { event, payload }\n if (event === undefined) delete data.event\n\n setState(prevState => {\n let count = (prevState.count || 0) + 1\n let result: UseForceUpdateState = { event, payload, count }\n return result\n }) // this triggers re-render\n }\n\n return state\n}\n"],"names":["emitter","mitt","getOnKeys","type","result","add","typeItem","push","Array","isArray","forEach","getEmitKey","initialState","count","runForceUpdate","props","p","emit","payload","useForceUpdate","React","useState","state","setState","keys","useMemo","useEffect","key","on","updateState","off","mittType","mittEvt","event","undefined","prevState"],"mappings":";;;;;;;;;AA4BA,IAAIA,OAAO,gBAAiBC,IAAI,EAAhC;;AAEA,IAAIC,SAAS,GAAG,SAAZA,SAAY,CAACC,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x"],"sourcesContent":["import React from 'react'\nimport mitt from 'mitt'\n\n// Please do not use types off of a default export module or else Storybook Docs will suffer.\n// see: https://github.com/storybookjs/storybook/issues/9556\n\ntype UseForceUpdatePropsItem = string | undefined\nexport type UseForceUpdateProps =\n | UseForceUpdatePropsItem\n | Array<UseForceUpdatePropsItem>\n\ntype Input = string | undefined\ntype RunForceUpdatePropsItem = {\n type?: Input\n payload?: any\n}\nexport type RunForceUpdateProps = Input | RunForceUpdatePropsItem\n\nexport type UseForceUpdateState = {\n count: number\n type?: string\n payload?: any\n}\n\nlet emitter: mitt.Emitter = mitt()\n\nlet getOnKeys = (type: UseForceUpdateProps): Array<string> => {\n let result = []\n\n function add(typeItem: Input) {\n if (typeItem === '*') result.push('*')\n else if (typeof typeItem === 'undefined') result.push('update_default')\n else result.push('update_' + typeItem)\n }\n\n if (Array.isArray(type)) {\n type.forEach(typeItem => {\n add(typeItem)\n })\n } else {\n add(type)\n }\n\n return result\n}\n\nlet getEmitKey = (type: Input): string => {\n if (typeof type === 'undefined') return 'update_default'\n\n return 'update_' + type\n}\n\nlet initialState: UseForceUpdateState = { count: 0 }\n\nexport function runForceUpdate(props?: RunForceUpdateProps) {\n if (typeof props === 'undefined' || typeof props === 'string') {\n emitter.emit(getEmitKey(props as Input))\n } else {\n let typedProps: RunForceUpdatePropsItem = props\n let payload = typedProps?.payload\n\n typeof payload === 'undefined'\n ? emitter.emit(getEmitKey(typedProps.type))\n : emitter.emit(getEmitKey(typedProps.type), payload)\n }\n}\n\nexport function useForceUpdate(type?: Input): UseForceUpdateState {\n const [state, setState] = React.useState(initialState)\n\n let keys = React.useMemo(() => {\n return getOnKeys(type)\n }, [])\n\n React.useEffect(() => {\n keys.forEach(key => {\n emitter.on(key, updateState)\n })\n\n return () => {\n keys.forEach(key => {\n emitter.off(key, updateState)\n })\n }\n })\n\n function updateState(mittType: any, mittEvt?: any) {\n // Order of args in mitt depending on whether type is '*' or not.\n // Either it is (mittType, mittEvt) or (mittEvt).\n // https://github.com/developit/mitt/blob/master/src/index.js\n\n let type = mittEvt ? mittType : undefined\n let payload = mittEvt ? mittEvt : mittType\n\n // this triggers re-render\n setState(prevState => {\n let count = (prevState.count || 0) + 1\n let result: UseForceUpdateState = { type, payload, count }\n if (type === undefined) delete result.type\n if (payload === undefined) delete result.payload\n\n return result\n })\n }\n\n return state\n}\n"],"names":["emitter","mitt","getOnKeys","type","result","add","typeItem","push","Array","isArray","forEach","getEmitKey","initialState","count","runForceUpdate","props","emit","typedProps","payload","useForceUpdate","React","useState","state","setState","keys","useMemo","useEffect","key","on","updateState","off","mittType","mittEvt","undefined","prevState"],"mappings":";;;;;;;;;AAwBA,IAAIA,OAAO,gBAAiBC,IAAI,EAAhC;;AAEA,IAAIC,SAAS,GAAG,SAAZA,SAAY,CAACC,IAAD;AACd,MAAIC,MAAM,GAAG,EAAb;;AAEA,WAASC,GAAT,CAAaC,QAAb;AACE,QAAIA,QAAQ,KAAK,GAAjB,EAAsBF,MAAM,CAACG,IAAP,CAAY,GAAZ,EAAtB,KACK,IAAI,OAAOD,QAAP,KAAoB,WAAxB,EAAqCF,MAAM,CAACG,IAAP,CAAY,gBAAZ,EAArC,KACAH,MAAM,CAACG,IAAP,CAAY,YAAYD,QAAxB;AACN;;AAED,MAAIE,KAAK,CAACC,OAAN,CAAcN,IAAd,CAAJ,EAAyB;AACvBA,IAAAA,IAAI,CAACO,OAAL,CAAa,UAAAJ,QAAQ;AACnBD,MAAAA,GAAG,CAACC,QAAD,CAAH;AACD,KAFD;AAGD,GAJD,MAIO;AACLD,IAAAA,GAAG,CAACF,IAAD,CAAH;AACD;;AAED,SAAOC,MAAP;AACD,CAlBD;;AAoBA,IAAIO,UAAU,GAAG,SAAbA,UAAa,CAACR,IAAD;AACf,MAAI,OAAOA,IAAP,KAAgB,WAApB,EAAiC,OAAO,gBAAP;AAEjC,SAAO,YAAYA,IAAnB;AACD,CAJD;;AAMA,IAAIS,YAAY,GAAwB;AAAEC,EAAAA,KAAK,E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;AAGAR,IAAAA,QAAQ,CAAC,UAAAW,SAAS;AAChB,UAAIrB,KAAK,GAAG,CAACqB,SAAS,CAACrB,KAAV,IAAmB,CAApB,IAAyB,CAArC;AACA,UAAIT,MAAM,GAAwB;AAAED,QAAAA,IAAI,EAAJA,IAAF;AAAQe,QAAAA,OAAO,EAAPA,OAAR;AAAiBL,QAAAA,KAAK,EAALA;AAAjB,OAAlC;AACA,UAAIV,IAAI,KAAK8B,SAAb,EAAwB,OAAO7B,MAAM,CAACD,IAAd;AACxB,UAAIe,OAAO,KAAKe,SAAhB,EAA2B,OAAO7B,MAAM,CAACc,OAAd;AAE3B,aAAOd,MAAP;AACD,KAPO,CAAR;AAQD;;AAED,SAAOkB,KAAP;AACD;;;;;"} |
@@ -1,2 +0,2 @@ | ||
| "use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=t(require("react")),n=t(require("mitt"))(),r=function(t){return void 0!==t?"update_"+t:"update"},u={count:0};exports.runForceUpdate=function(t){if(Array.isArray(t))t.forEach((function(t){var e=r(null==t?void 0:t.type);n.emit(e,null==t?void 0:t.payload)}));else{var e=r(null==t?void 0:t.type);n.emit(e,null==t?void 0:t.payload)}},exports.useForceUpdate=function(t){var r=e.useState(u),o=r[0],i=r[1],a=e.useMemo((function(){return function(t){var e=[];function n(t){e.push("*"===t?"*":void 0===t||""===t?"update":"update_"+t)}return Array.isArray(t)?t.forEach((function(t){n(t)})):n(t),e}(t)}),[]);function c(t,e){var n=e?t:void 0,r=e||t;i((function(t){return{event:n,payload:r,count:(t.count||0)+1}}))}return e.useEffect((function(){return a.forEach((function(t){n.on(t,c)})),function(){a.forEach((function(t){n.off(t,c)}))}})),o}; | ||
| "use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("react")),n=e(require("mitt"))(),o=function(e){return void 0===e?"update_default":"update_"+e},u={count:0};exports.runForceUpdate=function(e){if(void 0===e||"string"==typeof e)n.emit(o(e));else{var t=e,u=null==t?void 0:t.payload;void 0===u?n.emit(o(t.type)):n.emit(o(t.type),u)}},exports.useForceUpdate=function(e){var o=t.useState(u),r=o[0],i=o[1],a=t.useMemo((function(){return function(e){var t=[];function n(e){t.push("*"===e?"*":void 0===e?"update_default":"update_"+e)}return Array.isArray(e)?e.forEach((function(e){n(e)})):n(e),t}(e)}),[]);function f(e,t){var n=t?e:void 0,o=t||e;i((function(e){var t={type:n,payload:o,count:(e.count||0)+1};return void 0===n&&delete t.type,void 0===o&&delete t.payload,t}))}return t.useEffect((function(){return a.forEach((function(e){n.on(e,f)})),function(){a.forEach((function(e){n.off(e,f)}))}})),r}; | ||
| //# sourceMappingURL=react-forceupdate.cjs.production.min.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"react-forceupdate.cjs.production.min.js","sources":["../src/index.tsx"],"sourcesContent":["import React from 'react'\nimport mitt from 'mitt'\n\n// Please do not use types off of a default export module or else Storybook Docs will suffer.\n// see: https://github.com/storybookjs/storybook/issues/9556\n\ntype UseForceUpdatePropsItem = string | number | undefined\nexport type UseForceUpdateProps =\n | UseForceUpdatePropsItem\n | Array<UseForceUpdatePropsItem>\n\ntype Input = string | number | undefined\n\ntype RunForceUpdatePropsItem = {\n type?: Input\n payload?: any\n}\n\nexport type RunForceUpdateProps =\n | RunForceUpdatePropsItem\n | Array<RunForceUpdatePropsItem>\n\nexport type UseForceUpdateState = {\n count: number\n event?: string\n payload?: any\n}\n\nlet emitter: mitt.Emitter = mitt()\n\nlet getOnKeys = (type: UseForceUpdateProps): Array<string> => {\n let result = []\n\n function add(typeItem) {\n if (typeItem === '*') result.push('*')\n else if (typeof typeItem === 'undefined') result.push('update')\n else if (typeItem === '') result.push('update')\n else result.push('update_' + typeItem)\n }\n\n if (Array.isArray(type)) {\n type.forEach(typeItem => {\n add(typeItem)\n })\n } else {\n add(type)\n }\n\n return result\n}\nlet getEmitKey = (type: Input): string =>\n typeof type !== 'undefined' ? 'update_' + type : 'update'\n\nlet initialState: UseForceUpdateState = { count: 0 }\n\nexport function runForceUpdate(props?: RunForceUpdateProps) {\n if (Array.isArray(props)) {\n ;(props as Array<RunForceUpdatePropsItem>).forEach(p => {\n let type = getEmitKey(p?.type)\n emitter.emit(type, p?.payload)\n })\n } else {\n let type = getEmitKey(props?.type)\n\n emitter.emit(type, props?.payload)\n }\n}\n\nexport function useForceUpdate(type?: Input): UseForceUpdateState {\n const [state, setState] = React.useState(initialState)\n\n let keys = React.useMemo(() => {\n return getOnKeys(type)\n }, [])\n\n React.useEffect(() => {\n keys.forEach(key => {\n emitter.on(key, updateState)\n })\n\n return () => {\n keys.forEach(key => {\n emitter.off(key, updateState)\n })\n }\n })\n\n function updateState(mittType: any, mittEvt?: any) {\n // Cumbersome parsing due to the order of args in mitt depending on whether type is '*' or not.\n // Either it is (mittType, mittEvt) or (mittEvt).\n // https://github.com/developit/mitt/blob/master/src/index.js\n\n let event = mittEvt ? mittType : undefined\n let payload = mittEvt ? mittEvt : mittType\n let data = { event, payload }\n if (event === undefined) delete data.event\n\n setState(prevState => {\n let count = (prevState.count || 0) + 1\n let result: UseForceUpdateState = { event, payload, count }\n return result\n }) // this triggers re-render\n }\n\n return state\n}\n"],"names":["emitter","mitt","getEmitKey","type","initialState","count","props","Array","isArray","forEach","p","emit","payload","React","useState","state","setState","keys","useMemo","result","add","typeItem","push","getOnKeys","updateState","mittType","mittEvt","event","undefined","prevState","useEffect","key","on","off"],"mappings":"oKA4BIA,oBAAwBC,GAsBxBC,EAAa,SAACC,eACA,IAATA,EAAuB,UAAYA,EAAO,UAE/CC,EAAoC,CAAEC,MAAO,mCAElBC,MACzBC,MAAMC,QAAQF,GACdA,EAAyCG,SAAQ,SAAAC,OAC7CP,EAAOD,EAAWQ,MAAAA,SAAAA,EAAGP,MACzBH,EAAQW,KAAKR,EAAMO,MAAAA,SAAAA,EAAGE,gBAEnB,KACDT,EAAOD,EAAWI,MAAAA,SAAAA,EAAOH,MAE7BH,EAAQW,KAAKR,EAAMG,MAAAA,SAAAA,EAAOM,2CAICT,SACHU,EAAMC,SAASV,GAAlCW,OAAOC,OAEVC,EAAOJ,EAAMK,SAAQ,kBAzCX,SAACf,OACXgB,EAAS,YAEJC,EAAIC,GACWF,EAAOG,KAAZ,MAAbD,EAA8B,SACL,IAAbA,GACM,KAAbA,EAD6C,SAErC,UAAYA,UAG3Bd,MAAMC,QAAQL,GAChBA,EAAKM,SAAQ,SAAAY,GACXD,EAAIC,MAGND,EAAIjB,GAGCgB,EAwBEI,CAAUpB,KAChB,aAcMqB,EAAYC,EAAeC,OAK9BC,EAAQD,EAAUD,OAAWG,EAC7BhB,EAAUc,GAAoBD,EAIlCT,GAAS,SAAAa,SAE2B,CAAEF,MAAAA,EAAOf,QAAAA,EAASP,OADvCwB,EAAUxB,OAAS,GAAK,aAvBzCQ,EAAMiB,WAAU,kBACdb,EAAKR,SAAQ,SAAAsB,GACX/B,EAAQgC,GAAGD,EAAKP,MAGX,WACLP,EAAKR,SAAQ,SAAAsB,GACX/B,EAAQiC,IAAIF,EAAKP,UAsBhBT"} | ||
| {"version":3,"file":"react-forceupdate.cjs.production.min.js","sources":["../src/index.tsx"],"sourcesContent":["import React from 'react'\nimport mitt from 'mitt'\n\n// Please do not use types off of a default export module or else Storybook Docs will suffer.\n// see: https://github.com/storybookjs/storybook/issues/9556\n\ntype UseForceUpdatePropsItem = string | undefined\nexport type UseForceUpdateProps =\n | UseForceUpdatePropsItem\n | Array<UseForceUpdatePropsItem>\n\ntype Input = string | undefined\ntype RunForceUpdatePropsItem = {\n type?: Input\n payload?: any\n}\nexport type RunForceUpdateProps = Input | RunForceUpdatePropsItem\n\nexport type UseForceUpdateState = {\n count: number\n type?: string\n payload?: any\n}\n\nlet emitter: mitt.Emitter = mitt()\n\nlet getOnKeys = (type: UseForceUpdateProps): Array<string> => {\n let result = []\n\n function add(typeItem: Input) {\n if (typeItem === '*') result.push('*')\n else if (typeof typeItem === 'undefined') result.push('update_default')\n else result.push('update_' + typeItem)\n }\n\n if (Array.isArray(type)) {\n type.forEach(typeItem => {\n add(typeItem)\n })\n } else {\n add(type)\n }\n\n return result\n}\n\nlet getEmitKey = (type: Input): string => {\n if (typeof type === 'undefined') return 'update_default'\n\n return 'update_' + type\n}\n\nlet initialState: UseForceUpdateState = { count: 0 }\n\nexport function runForceUpdate(props?: RunForceUpdateProps) {\n if (typeof props === 'undefined' || typeof props === 'string') {\n emitter.emit(getEmitKey(props as Input))\n } else {\n let typedProps: RunForceUpdatePropsItem = props\n let payload = typedProps?.payload\n\n typeof payload === 'undefined'\n ? emitter.emit(getEmitKey(typedProps.type))\n : emitter.emit(getEmitKey(typedProps.type), payload)\n }\n}\n\nexport function useForceUpdate(type?: Input): UseForceUpdateState {\n const [state, setState] = React.useState(initialState)\n\n let keys = React.useMemo(() => {\n return getOnKeys(type)\n }, [])\n\n React.useEffect(() => {\n keys.forEach(key => {\n emitter.on(key, updateState)\n })\n\n return () => {\n keys.forEach(key => {\n emitter.off(key, updateState)\n })\n }\n })\n\n function updateState(mittType: any, mittEvt?: any) {\n // Order of args in mitt depending on whether type is '*' or not.\n // Either it is (mittType, mittEvt) or (mittEvt).\n // https://github.com/developit/mitt/blob/master/src/index.js\n\n let type = mittEvt ? mittType : undefined\n let payload = mittEvt ? mittEvt : mittType\n\n // this triggers re-render\n setState(prevState => {\n let count = (prevState.count || 0) + 1\n let result: UseForceUpdateState = { type, payload, count }\n if (type === undefined) delete result.type\n if (payload === undefined) delete result.payload\n\n return result\n })\n }\n\n return state\n}\n"],"names":["emitter","mitt","getEmitKey","type","initialState","count","props","emit","typedProps","payload","React","useState","state","setState","keys","useMemo","result","add","typeItem","push","Array","isArray","forEach","getOnKeys","updateState","mittType","mittEvt","undefined","prevState","useEffect","key","on","off"],"mappings":"oKAwBIA,oBAAwBC,GAsBxBC,EAAa,SAACC,eACI,IAATA,EAA6B,iBAEjC,UAAYA,GAGjBC,EAAoC,CAAEC,MAAO,mCAElBC,WACR,IAAVA,GAA0C,iBAAVA,EACzCN,EAAQO,KAAKL,EAAWI,QACnB,KACDE,EAAsCF,EACtCG,EAAUD,MAAAA,SAAAA,EAAYC,aAEP,IAAZA,EACHT,EAAQO,KAAKL,EAAWM,EAAWL,OACnCH,EAAQO,KAAKL,EAAWM,EAAWL,MAAOM,qCAInBN,SACHO,EAAMC,SAASP,GAAlCQ,OAAOC,OAEVC,EAAOJ,EAAMK,SAAQ,kBA5CX,SAACZ,OACXa,EAAS,YAEJC,EAAIC,GACWF,EAAOG,KAAZ,MAAbD,EAA8B,SACL,IAAbA,EAAsC,iBACrC,UAAYA,UAG3BE,MAAMC,QAAQlB,GAChBA,EAAKmB,SAAQ,SAAAJ,GACXD,EAAIC,MAGND,EAAId,GAGCa,EA4BEO,CAAUpB,KAChB,aAcMqB,EAAYC,EAAeC,OAK9BvB,EAAOuB,EAAUD,OAAWE,EAC5BlB,EAAUiB,GAAoBD,EAGlCZ,GAAS,SAAAe,OAEHZ,EAA8B,CAAEb,KAAAA,EAAMM,QAAAA,EAASJ,OADtCuB,EAAUvB,OAAS,GAAK,eAExBsB,IAATxB,UAA2Ba,EAAOb,UACtBwB,IAAZlB,UAA8BO,EAAOP,QAElCO,YA3BXN,EAAMmB,WAAU,kBACdf,EAAKQ,SAAQ,SAAAQ,GACX9B,EAAQ+B,GAAGD,EAAKN,MAGX,WACLV,EAAKQ,SAAQ,SAAAQ,GACX9B,EAAQgC,IAAIF,EAAKN,UAwBhBZ"} |
@@ -10,3 +10,3 @@ import React from 'react'; | ||
| function add(typeItem) { | ||
| if (typeItem === '*') result.push('*');else if (typeof typeItem === 'undefined') result.push('update');else if (typeItem === '') result.push('update');else result.push('update_' + typeItem); | ||
| if (typeItem === '*') result.push('*');else if (typeof typeItem === 'undefined') result.push('update_default');else result.push('update_' + typeItem); | ||
| } | ||
@@ -26,3 +26,4 @@ | ||
| var getEmitKey = function getEmitKey(type) { | ||
| return typeof type !== 'undefined' ? 'update_' + type : 'update'; | ||
| if (typeof type === 'undefined') return 'update_default'; | ||
| return 'update_' + type; | ||
| }; | ||
@@ -34,10 +35,8 @@ | ||
| function runForceUpdate(props) { | ||
| if (Array.isArray(props)) { | ||
| props.forEach(function (p) { | ||
| var type = getEmitKey(p === null || p === void 0 ? void 0 : p.type); | ||
| emitter.emit(type, p === null || p === void 0 ? void 0 : p.payload); | ||
| }); | ||
| if (typeof props === 'undefined' || typeof props === 'string') { | ||
| emitter.emit(getEmitKey(props)); | ||
| } else { | ||
| var type = getEmitKey(props === null || props === void 0 ? void 0 : props.type); | ||
| emitter.emit(type, props === null || props === void 0 ? void 0 : props.payload); | ||
| var typedProps = props; | ||
| var payload = typedProps === null || typedProps === void 0 ? void 0 : typedProps.payload; | ||
| typeof payload === 'undefined' ? emitter.emit(getEmitKey(typedProps.type)) : emitter.emit(getEmitKey(typedProps.type), payload); | ||
| } | ||
@@ -65,16 +64,19 @@ } | ||
| function updateState(mittType, mittEvt) { | ||
| // Cumbersome parsing due to the order of args in mitt depending on whether type is '*' or not. | ||
| // Order of args in mitt depending on whether type is '*' or not. | ||
| // Either it is (mittType, mittEvt) or (mittEvt). | ||
| // https://github.com/developit/mitt/blob/master/src/index.js | ||
| var event = mittEvt ? mittType : undefined; | ||
| var payload = mittEvt ? mittEvt : mittType; | ||
| var type = mittEvt ? mittType : undefined; | ||
| var payload = mittEvt ? mittEvt : mittType; // this triggers re-render | ||
| setState(function (prevState) { | ||
| var count = (prevState.count || 0) + 1; | ||
| var result = { | ||
| event: event, | ||
| type: type, | ||
| payload: payload, | ||
| count: count | ||
| }; | ||
| if (type === undefined) delete result.type; | ||
| if (payload === undefined) delete result.payload; | ||
| return result; | ||
| }); // this triggers re-render | ||
| }); | ||
| } | ||
@@ -81,0 +83,0 @@ |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"react-forceupdate.esm.js","sources":["../src/index.tsx"],"sourcesContent":["import React from 'react'\nimport mitt from 'mitt'\n\n// Please do not use types off of a default export module or else Storybook Docs will suffer.\n// see: https://github.com/storybookjs/storybook/issues/9556\n\ntype UseForceUpdatePropsItem = string | number | undefined\nexport type UseForceUpdateProps =\n | UseForceUpdatePropsItem\n | Array<UseForceUpdatePropsItem>\n\ntype Input = string | number | undefined\n\ntype RunForceUpdatePropsItem = {\n type?: Input\n payload?: any\n}\n\nexport type RunForceUpdateProps =\n | RunForceUpdatePropsItem\n | Array<RunForceUpdatePropsItem>\n\nexport type UseForceUpdateState = {\n count: number\n event?: string\n payload?: any\n}\n\nlet emitter: mitt.Emitter = mitt()\n\nlet getOnKeys = (type: UseForceUpdateProps): Array<string> => {\n let result = []\n\n function add(typeItem) {\n if (typeItem === '*') result.push('*')\n else if (typeof typeItem === 'undefined') result.push('update')\n else if (typeItem === '') result.push('update')\n else result.push('update_' + typeItem)\n }\n\n if (Array.isArray(type)) {\n type.forEach(typeItem => {\n add(typeItem)\n })\n } else {\n add(type)\n }\n\n return result\n}\nlet getEmitKey = (type: Input): string =>\n typeof type !== 'undefined' ? 'update_' + type : 'update'\n\nlet initialState: UseForceUpdateState = { count: 0 }\n\nexport function runForceUpdate(props?: RunForceUpdateProps) {\n if (Array.isArray(props)) {\n ;(props as Array<RunForceUpdatePropsItem>).forEach(p => {\n let type = getEmitKey(p?.type)\n emitter.emit(type, p?.payload)\n })\n } else {\n let type = getEmitKey(props?.type)\n\n emitter.emit(type, props?.payload)\n }\n}\n\nexport function useForceUpdate(type?: Input): UseForceUpdateState {\n const [state, setState] = React.useState(initialState)\n\n let keys = React.useMemo(() => {\n return getOnKeys(type)\n }, [])\n\n React.useEffect(() => {\n keys.forEach(key => {\n emitter.on(key, updateState)\n })\n\n return () => {\n keys.forEach(key => {\n emitter.off(key, updateState)\n })\n }\n })\n\n function updateState(mittType: any, mittEvt?: any) {\n // Cumbersome parsing due to the order of args in mitt depending on whether type is '*' or not.\n // Either it is (mittType, mittEvt) or (mittEvt).\n // https://github.com/developit/mitt/blob/master/src/index.js\n\n let event = mittEvt ? mittType : undefined\n let payload = mittEvt ? mittEvt : mittType\n let data = { event, payload }\n if (event === undefined) delete data.event\n\n setState(prevState => {\n let count = (prevState.count || 0) + 1\n let result: UseForceUpdateState = { event, payload, count }\n return result\n }) // this triggers re-render\n }\n\n return state\n}\n"],"names":["emitter","mitt","getOnKeys","type","result","add","typeItem","push","Array","isArray","forEach","getEmitKey","initialState","count","runForceUpdate","props","p","emit","payload","useForceUpdate","React","useState","state","setState","keys","useMemo","useEffect","key","on","updateState","off","mittType","mittEvt","event","undefined","prevState"],"mappings":";;;AA4BA,IAAIA,OAAO,gBAAiBC,IAAI,EAAhC;;AAEA,IAAIC,SAAS,GAAG,SAAZA,SAAY,CAACC,IAAD;AACd,MAAIC,MAAM,GAAG,EAAb;;AAEA,WAASC,GAAT,CAAaC,QAAb;AACE,QAAIA,QAAQ,KAAK,GAAjB,EAAsBF,MAAM,CAACG,IAAP,CAAY,GAAZ,EAAtB,KACK,IAAI,OAAOD,QAAP,KAAoB,WAAxB,EAAqCF,MAAM,CAACG,IAAP,CAAY,QAAZ,EAArC,KACA,IAAID,QAAQ,KAAK,EAAjB,EAAqBF,MAAM,CAACG,IAAP,CAAY,QAAZ,EAArB,KACAH,MAAM,CAACG,IAAP,CAAY,YAAYD,QAAxB;AACN;;AAED,MAAIE,KAAK,CAACC,OAAN,CAAcN,IAAd,CAAJ,EAAyB;AACvBA,IAAAA,IAAI,CAACO,OAAL,CAAa,UAAAJ,QAAQ;AACnBD,MAAAA,GAAG,CAACC,QAAD,CAAH;AACD,KAFD;AAGD,GAJD,MAIO;AACLD,IAAAA,GAAG,CAACF,IAAD,CAAH;AACD;;AAED,SAAOC,MAAP;AACD,CAnBD;;AAoBA,IAAIO,UAAU,GAAG,SAAbA,UAAa,CAACR,IAAD;AAAA,SACf,OAAOA,IAAP,KAAgB,WAAhB,GAA8B,YAAYA,IAA1C,GAAiD,QADlC;AAAA,CAAjB;;AAGA,IAAIS,YAAY,GAAwB;AAAEC,EAAAA,KAAK,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QAAIhB,OAAO,GAAGc,OAAO,GAAGA,OAAH,GAAaD,QAAlC;AAIAR,IAAAA,QAAQ,CAAC,UAAAY,SAAS;AAChB,UAAItB,KAAK,GAAG,CAACsB,SAAS,CAACtB,KAAV,IAAmB,CAApB,IAAyB,CAArC;AACA,UAAIT,MAAM,GAAwB;AAAE6B,QAAAA,KAAK,EAALA,KAAF;AAASf,QAAAA,OAAO,EAAPA,OAAT;AAAkBL,QAAAA,KAAK,EAALA;AAAlB,OAAlC;AACA,aAAOT,MAAP;AACD,KAJO,CAAR;AAKD;;AAED,SAAOkB,KAAP;AACD;;;;"} | ||
| {"version":3,"file":"react-forceupdate.esm.js","sources":["../src/index.tsx"],"sourcesContent":["import React from 'react'\nimport mitt from 'mitt'\n\n// Please do not use types off of a default export module or else Storybook Docs will suffer.\n// see: https://github.com/storybookjs/storybook/issues/9556\n\ntype UseForceUpdatePropsItem = string | undefined\nexport type UseForceUpdateProps =\n | UseForceUpdatePropsItem\n | Array<UseForceUpdatePropsItem>\n\ntype Input = string | undefined\ntype RunForceUpdatePropsItem = {\n type?: Input\n payload?: any\n}\nexport type RunForceUpdateProps = Input | RunForceUpdatePropsItem\n\nexport type UseForceUpdateState = {\n count: number\n type?: string\n payload?: any\n}\n\nlet emitter: mitt.Emitter = mitt()\n\nlet getOnKeys = (type: UseForceUpdateProps): Array<string> => {\n let result = []\n\n function add(typeItem: Input) {\n if (typeItem === '*') result.push('*')\n else if (typeof typeItem === 'undefined') result.push('update_default')\n else result.push('update_' + typeItem)\n }\n\n if (Array.isArray(type)) {\n type.forEach(typeItem => {\n add(typeItem)\n })\n } else {\n add(type)\n }\n\n return result\n}\n\nlet getEmitKey = (type: Input): string => {\n if (typeof type === 'undefined') return 'update_default'\n\n return 'update_' + type\n}\n\nlet initialState: UseForceUpdateState = { count: 0 }\n\nexport function runForceUpdate(props?: RunForceUpdateProps) {\n if (typeof props === 'undefined' || typeof props === 'string') {\n emitter.emit(getEmitKey(props as Input))\n } else {\n let typedProps: RunForceUpdatePropsItem = props\n let payload = typedProps?.payload\n\n typeof payload === 'undefined'\n ? emitter.emit(getEmitKey(typedProps.type))\n : emitter.emit(getEmitKey(typedProps.type), payload)\n }\n}\n\nexport function useForceUpdate(type?: Input): UseForceUpdateState {\n const [state, setState] = React.useState(initialState)\n\n let keys = React.useMemo(() => {\n return getOnKeys(type)\n }, [])\n\n React.useEffect(() => {\n keys.forEach(key => {\n emitter.on(key, updateState)\n })\n\n return () => {\n keys.forEach(key => {\n emitter.off(key, updateState)\n })\n }\n })\n\n function updateState(mittType: any, mittEvt?: any) {\n // Order of args in mitt depending on whether type is '*' or not.\n // Either it is (mittType, mittEvt) or (mittEvt).\n // https://github.com/developit/mitt/blob/master/src/index.js\n\n let type = mittEvt ? mittType : undefined\n let payload = mittEvt ? mittEvt : mittType\n\n // this triggers re-render\n setState(prevState => {\n let count = (prevState.count || 0) + 1\n let result: UseForceUpdateState = { type, payload, count }\n if (type === undefined) delete result.type\n if (payload === undefined) delete result.payload\n\n return result\n })\n }\n\n return state\n}\n"],"names":["emitter","mitt","getOnKeys","type","result","add","typeItem","push","Array","isArray","forEach","getEmitKey","initialState","count","runForceUpdate","props","emit","typedProps","payload","useForceUpdate","React","useState","state","setState","keys","useMemo","useEffect","key","on","updateState","off","mittType","mittEvt","undefined","prevState"],"mappings":";;;AAwBA,IAAIA,OAAO,gBAAiBC,IAAI,EAAhC;;AAEA,IAAIC,SAAS,GAAG,SAAZA,SAAY,CAACC,IAAD;AACd,MAAIC,MAAM,GAAG,EAAb;;AAEA,WAASC,GAAT,CAAaC,QAAb;AACE,QAAIA,QAAQ,KAAK,GAAjB,EAAsBF,MAAM,CAACG,IAAP,CAAY,GAAZ,EAAtB,KACK,IAAI,OAAOD,QAAP,KAAoB,WAAxB,EAAqCF,MAAM,CAACG,IAAP,CAAY,gBAAZ,EAArC,KACAH,MAAM,CAACG,IAAP,CAAY,YAAYD,QAAxB;AACN;;AAED,MAAIE,KAAK,CAACC,OAAN,C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d3B,MAAAA,OAAO,CAAC4B,EAAR,CAAWD,GAAX,EAAgBE,WAAhB;AACD,KAFD;AAIA,WAAO;AACLL,MAAAA,IAAI,CAACd,OAAL,CAAa,UAAAiB,GAAG;AACd3B,QAAAA,OAAO,CAAC8B,GAAR,CAAYH,GAAZ,EAAiBE,WAAjB;AACD,OAFD;AAGD,KAJD;AAKD,GAVD;;AAYA,WAASA,WAAT,CAAqBE,QAArB,EAAoCC,OAApC;AACE;AACA;AACA;AAEA,QAAI7B,IAAI,GAAG6B,OAAO,GAAGD,QAAH,GAAcE,SAAhC;AACA,QAAIf,OAAO,GAAGc,OAAO,GAAGA,OAAH,GAAaD,QAAlC;;AAGAR,IAAAA,QAAQ,CAAC,UAAAW,SAAS;AAChB,UAAIrB,KAAK,GAAG,CAACqB,SAAS,CAACrB,KAAV,IAAmB,CAApB,IAAyB,CAArC;AACA,UAAIT,MAAM,GAAwB;AAAED,QAAAA,IAAI,EAAJA,IAAF;AAAQe,QAAAA,OAAO,EAAPA,OAAR;AAAiBL,QAAAA,KAAK,EAALA;AAAjB,OAAlC;AACA,UAAIV,IAAI,KAAK8B,SAAb,EAAwB,OAAO7B,MAAM,CAACD,IAAd;AACxB,UAAIe,OAAO,KAAKe,SAAhB,EAA2B,OAAO7B,MAAM,CAACc,OAAd;AAE3B,aAAOd,MAAP;AACD,KAPO,CAAR;AAQD;;AAED,SAAOkB,KAAP;AACD;;;;"} |
+1
-1
| { | ||
| "version": "0.0.9", | ||
| "version": "0.0.10", | ||
| "license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
+26
-30
@@ -40,3 +40,3 @@ # react-forceupdate | ||
| let Lorem = () => { | ||
| let Alpha = () => { | ||
| useForceUpdate() // re-render on runForceUpdate event. | ||
@@ -47,3 +47,3 @@ | ||
| let Ipsum = () => { | ||
| let Bravo = () => { | ||
| useForceUpdate() // re-render on runForceUpdate event. | ||
@@ -66,4 +66,4 @@ | ||
| <button onClick={onForceUpdate}>Force update</button> | ||
| <DeeplyNestedComponentContainingLorem /> | ||
| <DeeplyNestedComponentContainingIpsum /> | ||
| <DeeplyNestedComponentContainingAlpha /> | ||
| <DeeplyNestedComponentContainingBravo /> | ||
| </main> | ||
@@ -74,3 +74,3 @@ ) | ||
| ### With type | ||
| ### With type or payload | ||
@@ -80,4 +80,4 @@ ```jsx | ||
| let Lorem = () => { | ||
| let data = useForceUpdate({ type: 'lorem' }) // re-render on runForceUpdate event. | ||
| let Alpha = () => { | ||
| let data = useForceUpdate('alpha') // re-render on runForceUpdate event. | ||
@@ -87,4 +87,4 @@ return <pre>{JSON.stringify(data, null, 2)}</pre> | ||
| let Ipsum = () => { | ||
| let data = useForceUpdate({ type: 'ipsum' }) // re-render on runForceUpdate event. | ||
| let Bravo = () => { | ||
| let data = useForceUpdate('bravo') // re-render on runForceUpdate event. | ||
@@ -94,4 +94,10 @@ return <pre>{JSON.stringify(data, null, 2)}</pre> | ||
| let AlphaOrBravo = () => { | ||
| let data = useForceUpdate(['alpha', 'bravo']) // re-render on runForceUpdate event. | ||
| return <pre>{JSON.stringify(data, null, 2)}</pre> | ||
| } | ||
| let CatchAll = () => { | ||
| let data = useForceUpdate({ type: '*' }) // re-render on runForceUpdate event. | ||
| let data = useForceUpdate('*') // re-render on runForceUpdate event. | ||
@@ -102,30 +108,20 @@ return <pre>{JSON.stringify(data, null, 2)}</pre> | ||
| function App() { | ||
| let onForceUpdateLorem = () => { | ||
| let onForceUpdateAlpha = () => { | ||
| // force update those who uses useForceUpdate hook with given type. | ||
| runForceUpdate({ type: 'lorem', payload: 'hi' }) | ||
| runForceUpdate({ type: 'alpha', payload: 'hi' }) | ||
| } | ||
| let onForceUpdateIpsum = () => { | ||
| let onForceUpdateBravo = () => { | ||
| // force update those who uses useForceUpdate hook with given type. | ||
| runForceUpdate({ type: 'ipsum' }) | ||
| runForceUpdate('bravo') | ||
| } | ||
| let onForceUpdateLoremIpsum = () => { | ||
| // force update those who uses useForceUpdate hook with given type. | ||
| runForceUpdate([ | ||
| { type: 'lorem', payload: { value: 1 } }, | ||
| { type: 'ipsum', payload: 'hello' }, | ||
| { payload: 'howdy' }, // default type | ||
| ]) | ||
| } | ||
| return ( | ||
| <main> | ||
| <button onClick={onForceUpdateLorem}>Force update lorem</button> | ||
| <button onClick={onForceUpdateIpsum}>Force update ipsum</button> | ||
| <button onClick={onForceUpdateLoremIpsum}> | ||
| Force update lorem and ipsum | ||
| </button> | ||
| <DeeplyNestedComponentContainingLorem /> | ||
| <DeeplyNestedComponentContainingIpsum /> | ||
| <button onClick={onForceUpdateAlpha}>Force update alpha</button> | ||
| <button onClick={onForceUpdateBravo}>Force update bravo</button> | ||
| <DeeplyNestedComponentContainingAlpha /> | ||
| <DeeplyNestedComponentContainingBravo /> | ||
| <DeeplyNestedComponentContainingAlphaBravoDefault /> | ||
| <DeeplyNestedComponentContainingCatchAll /> | ||
@@ -132,0 +128,0 @@ </main> |
+27
-26
@@ -7,3 +7,3 @@ import React from 'react' | ||
| type UseForceUpdatePropsItem = string | number | undefined | ||
| type UseForceUpdatePropsItem = string | undefined | ||
| export type UseForceUpdateProps = | ||
@@ -13,4 +13,3 @@ | UseForceUpdatePropsItem | ||
| type Input = string | number | undefined | ||
| type Input = string | undefined | ||
| type RunForceUpdatePropsItem = { | ||
@@ -20,10 +19,7 @@ type?: Input | ||
| } | ||
| export type RunForceUpdateProps = Input | RunForceUpdatePropsItem | ||
| export type RunForceUpdateProps = | ||
| | RunForceUpdatePropsItem | ||
| | Array<RunForceUpdatePropsItem> | ||
| export type UseForceUpdateState = { | ||
| count: number | ||
| event?: string | ||
| type?: string | ||
| payload?: any | ||
@@ -37,6 +33,5 @@ } | ||
| function add(typeItem) { | ||
| function add(typeItem: Input) { | ||
| if (typeItem === '*') result.push('*') | ||
| else if (typeof typeItem === 'undefined') result.push('update') | ||
| else if (typeItem === '') result.push('update') | ||
| else if (typeof typeItem === 'undefined') result.push('update_default') | ||
| else result.push('update_' + typeItem) | ||
@@ -55,17 +50,21 @@ } | ||
| } | ||
| let getEmitKey = (type: Input): string => | ||
| typeof type !== 'undefined' ? 'update_' + type : 'update' | ||
| let getEmitKey = (type: Input): string => { | ||
| if (typeof type === 'undefined') return 'update_default' | ||
| return 'update_' + type | ||
| } | ||
| let initialState: UseForceUpdateState = { count: 0 } | ||
| export function runForceUpdate(props?: RunForceUpdateProps) { | ||
| if (Array.isArray(props)) { | ||
| ;(props as Array<RunForceUpdatePropsItem>).forEach(p => { | ||
| let type = getEmitKey(p?.type) | ||
| emitter.emit(type, p?.payload) | ||
| }) | ||
| if (typeof props === 'undefined' || typeof props === 'string') { | ||
| emitter.emit(getEmitKey(props as Input)) | ||
| } else { | ||
| let type = getEmitKey(props?.type) | ||
| let typedProps: RunForceUpdatePropsItem = props | ||
| let payload = typedProps?.payload | ||
| emitter.emit(type, props?.payload) | ||
| typeof payload === 'undefined' | ||
| ? emitter.emit(getEmitKey(typedProps.type)) | ||
| : emitter.emit(getEmitKey(typedProps.type), payload) | ||
| } | ||
@@ -94,16 +93,18 @@ } | ||
| function updateState(mittType: any, mittEvt?: any) { | ||
| // Cumbersome parsing due to the order of args in mitt depending on whether type is '*' or not. | ||
| // Order of args in mitt depending on whether type is '*' or not. | ||
| // Either it is (mittType, mittEvt) or (mittEvt). | ||
| // https://github.com/developit/mitt/blob/master/src/index.js | ||
| let event = mittEvt ? mittType : undefined | ||
| let type = mittEvt ? mittType : undefined | ||
| let payload = mittEvt ? mittEvt : mittType | ||
| let data = { event, payload } | ||
| if (event === undefined) delete data.event | ||
| // this triggers re-render | ||
| setState(prevState => { | ||
| let count = (prevState.count || 0) + 1 | ||
| let result: UseForceUpdateState = { event, payload, count } | ||
| let result: UseForceUpdateState = { type, payload, count } | ||
| if (type === undefined) delete result.type | ||
| if (payload === undefined) delete result.payload | ||
| return result | ||
| }) // this triggers re-render | ||
| }) | ||
| } | ||
@@ -110,0 +111,0 @@ |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
30605
0.08%259
0.78%128
-3.03%