Atom effect (setSelf) "trigger" before default value
See original GitHub issueI’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:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top 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 >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
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.
Duplicate of #774