Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 586 Bytes

use-update-atom.mdx

File metadata and controls

27 lines (20 loc) · 586 Bytes
title
useUpdateAtom

Ref: pmndrs#26

For newer versions of Jotai you can also use useSetAtom instead.

import { atom, useAtom } from 'jotai'
import { useUpdateAtom } from 'jotai/utils'

const countAtom = atom(0)

const Counter = () => {
  const [count] = useAtom(countAtom)
  return <div>count: {count}</div>
}

const Controls = () => {
  const setCount = useUpdateAtom(countAtom)
  const inc = () => setCount((c) => c + 1)
  return <button onClick={inc}>+1</button>
}