You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
onDestroy is not called for every onCreate / onInit, resulting in the associated resources not being reclaimed. This happens when a component is instantiated multiple times and may be related to React Strict Mode.
Click the button repeatedly and see how the number of Hookstate instances goes to infinity.
Code:
importReactfrom"react";import{useHookstate,hookstate}from"@hookstate/core";exportdefaultfunctionApp(){constcomponent=useHookstate(true);constcount=useHookstate(instanceCounter);return(<divclassName="App"><buttononClick={()=>component.set((c)=>!c)}>Click</button>{component.get() ? <h1>Component1</h1>:<Component/>}NumberofHookstateinstancesfor`state`: {count.get()}</div>);}functionComponent(){conststate=useHookstate(2,countInstanceExtension);return<h1>Component{state.get()}</h1>;}constcountInstanceExtension=()=>({onInit: ()=>{// setTimeout is to avoid setting counter while rendering.setTimeout(()=>instanceCounter.set((x)=>x+1));},onDestroy: ()=>{setTimeout(()=>instanceCounter.set((x)=>x-1));},});letinstanceCounter=hookstate(0);
The text was updated successfully, but these errors were encountered:
Hi, very interesting. Hookstate can handle strict mode and can do the related double init/destroy. It seems like it is definitely caused by the strict mode.
I have debugged it. It happens only in StrictMode in development. The fix is possible but may affect extensions callbacks calls behavior. I will try to implement something.
onDestroy
is not called for everyonCreate
/onInit
, resulting in the associated resources not being reclaimed. This happens when a component is instantiated multiple times and may be related to React Strict Mode.Steps to reproduce:
Code:
The text was updated successfully, but these errors were encountered: