injectAtomInstance
import { injectAtomInstance } from '@zedux/react'
An injector that accepts an atom template and its params and registers a static graph dependency on the resolved atom instance. Returns the resolved atom instance object.
Since the dependency is static, the atom instance that uses this injector will not reevaluate when the resolved atom instance's state changes. However, it will reevaluate when the atom instance's promise changes or when the atom instance is destroyed.
You can also pass an atom instance directly to register a static graph dependency on instances received from other sources, e.g. an atom instance param.
Examples
Miscellaneous:
const instance = injectAtomInstance(myAtom)
const withParams = injectAtomInstance(myAtom, ['param 1', 'param 2'])
// passing an operation helps with debugging:
const injectCustomStuff = () => {
const instance = injectAtomInstance(myAtom, [], 'injectCustomStuff')
}
Signature
- Simplified
- TypeScript
injectAtomInstance = (atom, params?, operation?) => instance
declare const injectAtomInstance: {
<A extends AnyAtomTemplate>(
template: A,
params: AtomParamsType<A>,
config?: InjectAtomInstanceConfig
): AtomInstanceType<A>
<A extends AnyAtomTemplate>(
template: ParamlessTemplate<A>
): AtomInstanceType<A>
<I extends AnyAtomInstance>(
instance: I,
params?: [],
config?: InjectAtomInstanceConfig
): 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, injectAtomInstance()
returns the passed instance as-is.
In all cases, injectAtomInstance()
registers a static dependency on the resolved 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.
Optional. An object containing the following optional properties:
{ operation, subscribe }
A string. Default 'injectAtomInstance'
.
You usually won't need to worry about this. But passing an operation string can help with debugging e.g. if the instance throws an error.
A boolean. Default false
.
Whether to subscribe to state updates in the resolved atom instance, triggering a reevaluation of the current atom instance every time the resolved atom instance's state changes.
injectAtomInstance()
is "static" by default, meaning it won't subscribe to updates.
const val = injectAtomInstance(
myAtom,
[],
{ subscribe: true }
).getState()
// the above is equivalent to:
const val = injectAtomValue(myAtom)
The resolved atom instance.
Uses the passed atom template + params combo to find an existing atom instance. If no instance is found, creates one using the template and params and returns it.