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

See atom

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

Example

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

Parameters

NameDescription

name

string

valueOrInitialiser

(() => Value) | Value

options

AtomOptions<Value, Diff>

Returns

Atom<Value, Diff>
Prev
track
Next
useComputed

We use cookies on this website.
Learn more in our Cookie Policy.