onSet() handler in effect not triggered when atom initialized via RecoilRoot initializeState or snapshot
See original GitHub issueRecoilRoot using initializeState:
<RecoilRoot
initializeState={({ set }) => set(domainState, props.savedDomainState)}
>
<AppRoot root={props.root} />
</RecoilRoot>
The Atom using an effect:
const domainState = atom({
key: "domainState",
default: null,
effects_UNSTABLE: [syncDomainStorage],
});
The function: syncDomainStorage is only triggered on initial render when I use initializeState
, only if I remove initializeState
the side-effect function is called when the atom is updated.
Any updates to domainState
atom should be triggering the effect, which is not the behavior observed when used along with RecoilRoot initializeState
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:19 (6 by maintainers)
Top Results From Across the Web
Atom Effects | Recoil
Atom effects are an API for managing side-effects and synchronizing or initializing Recoil atoms. They have a variety of useful applications such as...
Read more >recoil: Versions | Openbase
Fix useRecoilSnapshot() with React's Fast Refresh during development (#1891) ... Call onSet() when atoms are initialized with <RecoilRoot initializeState={.
Read more >recoil | Yarn - Package Manager
Workaround for React 18 environments with nested renderers that don't support useSyncExternalStore() (#2001, #2010); Expose flag to disable "duplicate atom ...
Read more >Recoil指南--Atom Effects Atom副作用 - Lee前端技术栈
Atom Effects 是为了管理副作用和初始化Recoil Atom的实验性的API。 ... The effect will not call the onSet() handler when changed via that ...
Read more >How to subscribe to changes of snapshot (state) of Recoiljs in ...
Tried recoil-sync but did not received event in 'write' event handler prop in webcomponent. It only works inside RecoilRoot and not outside.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey! Thanks for reply @drarmstr .
I’m using cookies from request header to set Atoms state on initial Server-side render. And I want
effects_UNSTABLE onSet()
to mutate cookie client-side when state changes.My problem is probably framework-specific since nextjs framework gets ServerSide parameters and passes them to components like this
<App {...serversideprops}></App>
soinitializeState
is very helpful. And I couldn’t figure out how to pass current cookie header from request to atomeffects_UNSTABLE setSelf
😦Would’ve been perfect combo to have peristent state in cookies if only
onSet()
ineffects_UNSTABLE
could be used withinitialState
. I’m just using regularuseEffect
to set a cookie after changing atom state for now.Added example here.
_app.js
- here I pass cookies to initializeState on initial server-side render. (BreakssetSelf()
)index.js
- example of current behaviour.Should be fixed with #1511 and tested with #1519 for 0.6 release.
Note you’ll also be able to initialize atoms using props with the upcoming
recoil-sync
library and effects instead ofinitializeState
. (https://github.com/facebookexperimental/Recoil/pull/1462/files)