UNPKG

@zedux/react

Version:

A Molecular State Engine for React

42 lines (41 loc) 1.97 kB
import { AnyAtomInstance, AnyAtomTemplate, AtomInstanceType, AtomParamsType, ParamlessTemplate } from '@zedux/atoms'; import { ZeduxHookConfig } from '../types.js'; /** * Creates an atom instance for the passed atom template based on the passed * params. If an instance has already been created for the passed params, reuses * the existing instance. * * Registers a static graph dependency on the atom instance. This means * components that use this hook will not rerender when this atom instance's * state changes. * * If the atom doesn't take params or an instance is passed, pass an empty array * for the 2nd param when you need to supply the 3rd `config` param. * * The 3rd `config` param is an object with these fields: * * - `operation` - Used for debugging. Pass a string to describe the reason for * creating this graph edge * - `subscribe` - Pass `subscribe: true` to make `useAtomInstance` create a * dynamic graph dependency instead * - `suspend` - Pass `suspend: false` to prevent this hook from triggering * React suspense if the resolved atom has a promise set * * Note that if the params are large, serializing them every render can cause * some overhead. * * @param atom The atom template to instantiate or reuse an instantiation of OR * an atom instance itself. * @param params The params for generating the instance's key. Required if an * atom template is passed that requires params. * @param config An object with optional `operation`, `subscribe`, and `suspend` * fields. */ export declare const useAtomInstance: { <A extends AnyAtomTemplate>(template: A, params: AtomParamsType<A>, config?: ZeduxHookConfig): AtomInstanceType<A>; <A extends AnyAtomTemplate<{ Params: []; }>>(template: A): AtomInstanceType<A>; <A extends AnyAtomTemplate>(template: ParamlessTemplate<A>): AtomInstanceType<A>; <I extends AnyAtomInstance>(instance: I, params?: [], config?: ZeduxHookConfig): I; };