useAtom

Function: useAtom()

ts
function useAtom<TValue>(atom, options?): [Accessor<TValue>, (fn) => void & (value) => void];
function useAtom<TValue>(atom, options?): [Accessor<TValue>, (fn) => void & (value) => void];

Defined in: solid-store/src/useAtom.ts:23

Returns the current atom accessor together with a setter.

Use this when a component needs to both read and update the same writable atom.

Type Parameters

TValue

TValue

Parameters

atom

Atom<TValue>

options?

UseSelectorOptions<TValue>

Returns

[Accessor<TValue>, (fn) => void & (value) => void]

Example

tsx
const [count, setCount] = useAtom(countAtom)

return (
  <button type="button" onClick={() => setCount((prev) => prev + 1)}>
    {count()}
  </button>
)
const [count, setCount] = useAtom(countAtom)

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

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.