injectAtomValue
import { injectAtomValue } from '@zedux/react'
An injector that accepts an atom template and its params and registers a dynamic graph dependency on the resolved atom instance. Returns the current value of the injected atom instance's store.
The atom instance that uses this injector will reevaluate every time the resolved atom instance's state changes.
This injector is essentially an alias for:
const { get } = injectAtomGetters()
const value = get(otherAtom)
Except more restrictive since this is a normal injector while injectAtomGetters()
is unrestricted and get
is not an injector at all.
Example
Miscellaneous:
const currentState = injectAtomValue(myAtom)
const passingParams = injectAtomValue(myAtom, ['param 1', 'param 2'])
// passing an atom instance registers a dynamic dependency on that instance
const passingAnInstance = injectAtomValue(instance)
Signature
- Simplified
- TypeScript
injectAtomValue = (atom, params?) => state
declare const injectAtomValue: {
<A extends AnyAtomTemplate>(
template: A,
params: AtomParamsType<A>
): AtomStateType<A>
<A extends AnyAtomTemplate>(template: ParamlessTemplate<A>): AtomStateType<A>
<I extends AnyAtomInstance>(instance: I): AtomStateType<I>
}
Required. An atom template or atom instance.
If an atom template is passed, you must also pass any required params of the atom.
If an atom instance is passed, the params are ignored. In this case, injectAtomValue()
returns the current value of the passed instance (essentially instance.getState()
).
In all cases, injectAtomValue()
adds a dynamic dependency on the resolved atom instance.
Required if the passed atom template takes required params. Optional if not.
Don't pass this or pass an empty array if the atom does not take params or if passing an atom instance.
The current value of the resolved atom instance's store.
Uses the passed atom template + params combo to find an existing atom instance. If no instance is found, injectAtomValue()
creates one using the template and params and returns its initial value.