EvaluationReason
Every update that causes an atom instance to reevaluate produces an EvaluationReason. You can get the list of reasons that caused an atom instance to reevaluate using injectWhy()
. To get the reason list for selectors, use ecosystem.why()
.
Definition
interface EvaluationReason<State = any> {
action?: ActionChain
newState?: State
oldState?: State
operation: string // e.g. a function like "injectAtomValue"
sourceType: EvaluationSourceType
sourceId?: string // e.g. an instance id like "todos" or "userData-[1]"
reasons?: EvaluationReason[]
type: EvaluationType
}
type EvaluationSourceType =
| 'Atom'
| 'AtomSelector'
| 'External'
| 'Injector'
| 'Store'
type EvaluationType =
| 'cache invalidated'
| 'node destroyed'
| 'promise changed'
| 'state changed'
If this EvaluationReason was a store's state updating, then this will be a reference to the ActionChain object that caused the update. Otherwise, will be undefined.
The source's new state after updating. If the source is a store, this is the new state of the store. If the source is an atom instance, it's the new state of the atom instance's store. If the source is external (e.g. an invalidation from a React component), this will be undefined.
The source's state before updating. If the source is a store, this is the previous state of the store. If the source is an atom instance, it's the old state of the atom instance's store. If the source is external, this will be undefined.
The name of the function triggering this evaluation. If the source is an atom, injector, or store, operation
will be the function name you called to create the dynamic graph dependency - e.g. an injector name like "injectStore" or "injectAtomValue". If the source is external, the operation name will be "invalidate".
If the source dependency was another atom instance or selector, this will be that instance's or selector's id. Otherwise, this will be undefined.
What type of thing changed. Could be a store subscribed to via injectStore()
, an injected atom instance, or an .invalidate()
call from a dependent.
See the "EvaluationSourceType" definition above.
An array of EvaluationReasons. If the source dependency itself was updated in response to one of its dependencies updating, those EvaluationReasons will be listed here.
This is recursive! With this, you can see an update that originated many layers deep in the dependency tree and ultimately led to this atom reevaluating. If the source dependency was the instigator of the change, then this will be undefined.
The type of update that occurred. Could be a store's state changing, an atom instance being destroyed, an atom instance's promise changing, or an invalidation triggered externally.
See the "EvaluationType" definition above.