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.

Atom effect (setSelf) "trigger" before default value

See original GitHub issue

I’m getting a strange behavior, if I have a synchronous effect setting a value, I get the default value instead of the setted by the effect.

Consider this example:

const potatoAtom = atom({
  key: 'potato',
  default: false,
  effects_UNSTABLE: [({ setSelf }) => {
    setSelf(true);
  }]
});

function Component() {
  const potato = useRecoilValue(potatoAtom);

  console.log(potato); // false instead of true :(
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
drarmstrcommented, Nov 17, 2020

Calling setSelf() in an effect will initialize the atom state to a value. However, it does not change the default. So, if you reset an atom then it will revert to the default value instead of the initialized value. This is important to allow state persistence mechanisms to initialize to some saved value while also retaining default value behaviour.

Effects are only run when an atom is initialized. Note that atoms may be garbage collected and then re-initialized, which would cause the effect to re-run. However, that is not coupled to reseting the value to the default.

0reactions
drarmstrcommented, Dec 1, 2020

Duplicate of #774

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 ... initial value of the atom, or asynchronously called later to change...
Read more >
localStorage is not defined - Recoil with Next.js - Stack Overflow
Here you can try this. Worked for me import { recoilPersist } from 'recoil-persist' const localStorage = typeof window !==
Read more >
Jotai vs. Recoil: What are the differences? - LogRocket Blog
Also, for advanced use cases, you can use setSelf from effects_UNSTABLE instead of default (with trigger get ). The recoil selector is ...
Read more >
Atom creators - Jotai
atomWithToggle creates a new atom with a boolean as initial state & a setter function to toggle it. This avoids the boilerplate of...
Read more >
schrodinger.maestro.maestro module - Schrödinger
This only stays in effect for the next call to maestro.command(). ... By default the 'copy' parameter is True and a copy of...
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