useSetAtom is a React hook that returns a function to update an atom’s value. Unlike useAtom, it doesn’t subscribe to the atom’s value, so it won’t trigger re-renders when the atom changes.
When to use it
UseuseSetAtom when you only need to update an atom without reading its current value. This is more efficient than useAtom because it doesn’t create a subscription.
Signature
Parameters
atom: A writable atom to get the setter function foroptions: Optional configuration objectstore: Custom store to use (defaults to the store from Provider)
Returns
A stable setter function that accepts the atom’s write arguments and returns the write result.Basic Usage
countAtom changes, making it more efficient than using useAtom.
With Write-Only Atoms
useSetAtom is perfect for write-only atoms that perform side effects:
With Custom Arguments
Atoms can accept custom arguments in their write function:Comparison with useAtom
useSetAtom when:
- You only need to update the atom
- You want to prevent unnecessary re-renders
- You’re creating action/command atoms
Stable References
The setter function returned byuseSetAtom is stable and won’t change between renders (unless the atom or store changes):
TypeScript
useSetAtom preserves type safety for atom write arguments: