injectWhy
import { injectWhy } from '@zedux/react'
An unrestricted injector that returns a list of reasons explaining why the current atom instance is reevaluating. This injector returns an empty array on the first evaluation. On subsequent evaluations, it returns the full list of EvaluationReasons detailing why this atom reevaluated.
injectWhy()
is essentially an alias for:
const { ecosystem } = injectAtomGetters()
const reasons = ecosystem.why()
(Note that injectAtomGetters()
is also an unrestricted injector).
Evaluation reasons can be nested indefinitely. They can tell you about a store state change many atoms deep in the dependency tree of this atom instance.
Deconstructing Batches
When batching updates, Zedux only reevaluates atoms once - after all batched updates are processed.
When that evaluation happens, injectWhy
can be used to get the list of intermediate state changes that ultimately resulted in this evaluation but that didn't individually cause separate evaluations.
Examples
Getting the list of reasons.
injectWhy()
will return an empty array on the first evaluation. That's the only time. Thus injectWhy
can be used to determine if this is the first evaluation.
const reasons = injectWhy()
if (!reasons.length) {
// it's the first evaluation!
}
Signature
- Simplified
- TypeScript
injectWhy = () => evaluationReasons
declare const injectWhy: () => EvaluationReason<any>[]
The list of EvaluationReasons for this reevaluation.
Returns an empty array if this is the first time the current atom instance is being evaluated.