SetAtom
⚠️
<SetAtom/>
is experimental feature, this interfaces could be changed
The SetAtom component provides the same props interface as Jotai's useSetAtom (opens in a new tab) hook and can be used declaratively.
props.atom
You can use Jotai's atom as is.
import { SetAtom } from '@suspensive/jotai'
import { atom } from "jotai";
const switchAtom = atom(false)
const Example = () => (
<SetAtom atom={switchAtom}>
{(setCount) => (
<>
<button onClick={() => setCount(true)}>Set True</button>
<button onClick={() => setCount(false)}>Set False</button>
</>
)}
</SetAtom>
)
For Async Atom, Suspense is triggered until the Promise is resolved.
import { SetAtom } from '@suspensive/jotai'
import { atom } from "jotai";
const request = async () => fetch('https://...').then((res) => res.json())
const baseAtom = atom(0)
const Example = () => (
<SetAtom atom={baseAtom}>
{(setValue) => (
<>
<button onClick={() => setValue(request())}>request</button> // Suspend is triggered until the promise is resolved.
</>
)}
</SetAtom>
)