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
I'm looking for feedback on a change that replaces Atom Effects (which rely on a microtask delay) with Synchronous Atom Effects (which run in the same task). As with Atom Effects, Synchronous Atom Effects still batch atom updates, but they eliminate the need to wait for the next microtask. They also avoid extra renders and guarantee that they run before store subscriptions (i.e., useAtom, useAtomValue, or atom.onMount). With synchronous atom effects, you have a strong guarantee that the next value will always be correct.
Atom Effect
constcountAtom=atom(0)consteffectAtom=atomEffect((get,set)=>{constcount=get(countAtom)set(countAtom,2)})conststore=createStore()store.sub(effectAtom,()=>{})// mounts effectstore.set(countAtom,1)console.log(store.get(countAtom))// 1awaitPromise.resolve()// wait a microtaskconsole.log(store.get(countAtom))// 2
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm looking for feedback on a change that replaces Atom Effects (which rely on a microtask delay) with Synchronous Atom Effects (which run in the same task). As with Atom Effects, Synchronous Atom Effects still batch atom updates, but they eliminate the need to wait for the next microtask. They also avoid extra renders and guarantee that they run before store subscriptions (i.e.,
useAtom
,useAtomValue
, oratom.onMount
). With synchronous atom effects, you have a strong guarantee that the next value will always be correct.Atom Effect
Synchronous Atom Effect
Migration
If you still need to wait a microtask for certain use cases, you can manually queue one with queueMicrotask or
Promise.resolve().then
Beta Was this translation helpful? Give feedback.
All reactions