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
Actually, the MyStoreType is much more complex than the example above.
Now in a consumer component, I want to use 'currentColor' and 'currentView' property, and I don't want re-renders caused by the 'currentTool' property. I tried to wrap a more advanced selector, this selector looks like this:
/** Use this hook, get all the values of the context. */functionuseHook(): Readonly<MyStoreType&{Update: /* something else */}>/** Use a selector function to select a part of the context. */functionuseHook<TSelectedObject>(selector: (value: TFullValue)=>TSelectedObject): Readonly<TSelectedObject>&{Update: /* something else */}>/** Use an array of properties to select a part of the context. */functionuseHook<UextendskeyofTFullValue>(selections: Array<U>): Pick<TFullValue,U>&{Update: /* something else */}>functionuseHook(selector?: any){constselectFunc=useCallback((v: MyStoreType>|{v: {current: MyStoreType;}})=>{constvalue="v"inv ? v.v.current : v;if(selector===undefined){returnvalue;}else{letselection={};if(typeofselector==="function"){selection=selector(value);}elseif(Array.isArray(selector)){for(constkeyofselector){selection[key]=value[key];}}return{
...selection,Update: /* Some additional stuff */}}},[selector]);constvalue=useContextSelector(context,selectFunc);returnvalue;}
However when I pass an array to this hook, a infinite-loop exception will be thrown.
Hey,
I have a context object that contains multiple things:
Actually, the MyStoreType is much more complex than the example above.
Now in a consumer component, I want to use 'currentColor' and 'currentView' property, and I don't want re-renders caused by the 'currentTool' property. I tried to wrap a more advanced selector, this selector looks like this:
However when I pass an array to this hook, a infinite-loop exception will be thrown.
Can the use above be implemented in this way, or another way?
Thanks for ur time!
The text was updated successfully, but these errors were encountered: