Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

useCreateAtom.md

102 lines1.9 KB

Call Signature

function useCreateAtom<T>(getValue, options?): ReadonlyAtom<T>;

Defined in: node_modules/.pnpm/@tanstack+preact-store@0.13.2_preact@10.29.8/node_modules/@tanstack/preact-store/dist/useCreateAtom.d.ts:25

Creates a stable atom instance for the lifetime of the component.

Pass an initial value to create a writable atom, or a getter function to create a readonly derived atom. This mirrors createAtom, but only creates the atom once per component mount.

Type Parameters

T

T

Parameters

getValue

(prev?) => T

options?

AtomOptions<T>

Returns

ReadonlyAtom<T>

Example

function Counter() {
  const countAtom = useCreateAtom(0)
  const [count, setCount] = useAtom(countAtom)

  return (
    <button type="button" onClick={() => setCount((prev) => prev + 1)}>
      {count}
    </button>
  )
}

Call Signature

function useCreateAtom<T>(initialValue, options?): Atom<T>;

Defined in: node_modules/.pnpm/@tanstack+preact-store@0.13.2_preact@10.29.8/node_modules/@tanstack/preact-store/dist/useCreateAtom.d.ts:26

Creates a stable atom instance for the lifetime of the component.

Pass an initial value to create a writable atom, or a getter function to create a readonly derived atom. This mirrors createAtom, but only creates the atom once per component mount.

Type Parameters

T

T

Parameters

initialValue

T

options?

AtomOptions<T>

Returns

Atom<T>

Example

function Counter() {
  const countAtom = useCreateAtom(0)
  const [count, setCount] = useAtom(countAtom)

  return (
    <button type="button" onClick={() => setCount((prev) => prev + 1)}>
      {count}
    </button>
  )
}