title | description | nav |
---|---|---|
TypeScript |
How to use Jotai with TypeScript |
3.01 |
Jotai uses TypeScript 3.8+ syntax. Upgrade your TypeScript version if you're on 3.7.5 or lower.
Jotai relies heavily on type inferences and requires strictNullChecks
to be enabled. Consider adding "strict": true
in your tsconfig.json.
#550
#802
#838
const numAtom = atom(0) // primitive number atom
const strAtom = atom('') // primitive string atom
const numAtom = atom<number>(0)
const numAtom = atom<number | null>(0)
const arrAtom = atom<string[]>([])
const asyncStrAtom = atom(async () => "foo")
const writeOnlyAtom = atom(null, (_get, set, str: string) => set(fooAtom, str)
const readWriteAtom = atom<string, number>(
get => get(strAtom),
(_get, set, num) => set(strAtom, String(num))
)
const [num, setNum] = useAtom(primitiveNumAtom)
const [num] = useAtom(readOnlyNumAtom)
const [, setNum] = useAtom(writeOnlyNumAtom)