Skip to main content

useAtom()

Creates a new atom and returns it. The atom will be created only once.

See signia.atom

Example

const Counter = track(function Counter () {
const count = useAtom('count', 0)
const increment = useCallback(() => count.set(count.value + 1), [count])
return <button onClick={increment}>{count.value}</button>
})

Signature

useAtom<Value, Diff>(name: string, valueOrInitialiser: Value | () => Value, options?: AtomOptions<Value, Diff>): Atom<Value, Diff>;

Type parameters

  • Value
  • Diff = unknown

Parameters

NameTypeDescription
namestringThe name of the atom. This does not need to be globally unique. It is used for debugging and performance profiling.
valueOrInitialiserValue | () => ValueThe initial value of the atom. If this is a function, it will be called to get the initial value.
options?AtomOptions<Value, Diff>Options for the atom.

Returns

Atom<Value, Diff>

Defined in: signia-react/src/useAtom.ts:20