export { type Atom, createAtom }; /** * An atom is a value that can be subscribed to and updated. * * @param T - The type of the value * @returns The atom */ type Atom = { /** The current value. */ readonly value: T; /** Subscribe to the value. */ use: () => T; /** Set the value. */ set: AtomSetState; /** Reset the value to the default value. */ reset: () => void; /** Subscribe to the value.with a callback function. */ subscribe: (listener: (value: T) => void) => () => void; /** Compute a derived value from the current value, similar to useState + useMemo */ useCompute: (fn: (value: T) => R, deps?: readonly unknown[]) => R; }; type AtomSetState = (value: T | ((prev: T) => T)) => void; /** * Creates an atom with a given id and default value. * * @param id - The id of the atom * @param defaultValue - The default value of the atom * @returns The atom * @example * const stateA = createAtom(useId(), false) * return ( * <> * * * * {(value, setValue) => ( * * )} * * * * * ) */ declare function createAtom(id: string, defaultValue: T, persistent?: boolean): Atom;