question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onSet() handler in effect not triggered when atom initialized via RecoilRoot initializeState or snapshot

See original GitHub issue

RecoilRoot 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:open
  • Created 3 years ago
  • Reactions:8
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
notnooblordcommented, Feb 23, 2021

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> so initializeState is very helpful. And I couldn’t figure out how to pass current cookie header from request to atom effects_UNSTABLE setSelf 😦

Would’ve been perfect combo to have peristent state in cookies if only onSet() in effects_UNSTABLE could be used with initialState. I’m just using regular useEffect 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. (Breaks setSelf()) index.js - example of current behaviour.

4reactions
drarmstrcommented, Dec 31, 2021

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 of initializeState. (https://github.com/facebookexperimental/Recoil/pull/1462/files)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found