useSetAtom
Signature
useSetAtom<T>(atom: Atom<T>): SetterFn<T>valdres-react Write-only access to an atom
Returns a setter function for an atom without subscribing to its value. The component won't re-render when the atom changes — useful for components that only need to trigger updates.
Usage
import { atom } from "valdres"
import { useSetAtom } from "valdres-react"
const countAtom = atom(0)
function IncrementButton() {
const setCount = useSetAtom(countAtom)
return <button onClick={() => setCount(c => c + 1)}>+</button>
}
Why use this over useAtom?
If a component only writes to an atom but never displays its value, useSetAtom avoids subscribing to changes. This means the component won't re-render when the atom updates, which can improve performance.
See also
- useAtom — read + write hook
- useResetAtom — reset to default value