jotaijs / jotai-scope

MIT License
60 stars 4 forks source link

ScopeProvider calling referenced atom multiple times for each ScopeProvider intance #22

Closed jsheely closed 9 months ago

jsheely commented 9 months ago

I'm still working on how to model this properly.

But I have a ScopeProvider inside of a list where one of the atoms is scoped for this list item and then further down the tree I reference a global atom that references the scoped atom. All of which is working fine to yielding the correct data

HOWEVER it seems that when I update another referenced atom inside the global atom it causes the ScopeProvider to re-render and calling the getter of the global atom for each iteration yielding the same results redundantly. Normally the getter memoized if nothing has changed not get called.

ex. If list has 10 items. The atom getter will be called for the 1st element 10 times, 2nd item 10 times, etc. etc.

I'm not sure why it's triggering this behavior but I will update when I have a better example demo example.

jsheely commented 9 months ago

I realized when tracing. This may also be caused in conjunction with my usage of jotai-optics focusAtom in this context.

I will try and isolate that out.

jsheely commented 9 months ago

I'm still not sure this is desired behavior but in my case I was setting passing global atom value obtained from outside of the scope and passing it into a hook below the scope. Then using a useEffect to update the scoped version of the atom.

The idea was sort of a global override of individual list items. But this useEffect is what is causing the 10x10 behavior.

jsheely commented 9 months ago

Closing issue for now. I believe this may be a logic problem in my system and behavior is working as intended. Unable to replicate the exact scenario in clean app.