Recoil: Atom Effect - onSet() - renaming suggestion

Created on 30 Oct 2020  路  4Comments  路  Source: facebookexperimental/Recoil

Within an Atom Effect - the onSet() listener is fired on every access with the trigger being either 'set' or 'get'.

Since this listener function is called every access, both on set and on get - would it be better to name it onAccess or listener similar?

Cheers B

invalid question

All 4 comments

The onSet() listener is only called when the atom value is changed. The atom effect function itself is called when the atom is initialized on first use, which may be either due to a set or a get.

@drarmstr

Apologies for continued questions here - but in the case that an atom is used, then reset - either using useResetRecoilState(state) or resetSelf() - would subsequent usage of this atom be considered 'first use' and would the effect function run?

@drarmstr

Apologies for continued questions here - but in the case that an atom is used, then reset - either using useResetRecoilState(state) or resetSelf() - would subsequent usage of this atom be considered 'first use' and would the effect function run?

It's not guaranteed. If you reset an atom or if no one is subscribing to an atom, then they are candidates to be released and can be cleaned up, though they may not be. If that happens, then subsequent usage would re-initialize the atom and call the effect function again. This is similar to a React effect for a component. So, for both cases, it is important to carefully return a cleanup handler and be ready to handle the effect being called multiple times.

Added documentation with #680

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eLeontev picture eLeontev  路  3Comments

Sawtaytoes picture Sawtaytoes  路  4Comments

robsoncezario picture robsoncezario  路  3Comments

jamiebuilds picture jamiebuilds  路  3Comments

yuantongkang picture yuantongkang  路  3Comments