UNPKG

@zedux/react

Version:

A Molecular State Engine for React

37 lines (36 loc) 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useAtomValue = void 0; const useAtomInstance_1 = require("./useAtomInstance"); /** * 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 dynamic graph dependency on the atom instance. This means * components that use this hook will rerender when the resolved atom instance's * state changes. * * Also accepts an atom instance and subscribes to updates in the passed * instance. * * 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. * * Returns the current state of the resolved atom instance. * * The 3rd param is a `config` object which can be given `operation` and * `suspend` fields: * * ```ts * const state = useAtomValue(myAtom, [], { * operation: 'myUsageDescription', // helps with debugging * suspend: false, // disable React suspense if the atom set a promise * }) * ``` */ const useAtomValue = (atom, params, config = { operation: 'useAtomValue' }) => { const instance = (0, useAtomInstance_1.useAtomInstance)(atom, params, Object.assign(Object.assign({}, config), { subscribe: true })); return instance.getState(); }; exports.useAtomValue = useAtomValue;