Skip to main content
Module

x/jotai/src/core/useAtom.ts

👻 Primitive and flexible state management for React
Go to Latest
File
import type { Atom, Scope, SetAtom, WritableAtom } from './atom'import { useAtomValue } from './useAtomValue'import { useSetAtom } from './useSetAtom'
type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
export function useAtom<Value, Update, Result extends void | Promise<void>>( atom: WritableAtom<Value, Update, Result>, scope?: Scope): [Awaited<Value>, SetAtom<Update, Result>]
export function useAtom<Value>( atom: Atom<Value>, scope?: Scope): [Awaited<Value>, never]
export function useAtom<Value, Update, Result extends void | Promise<void>>( atom: Atom<Value> | WritableAtom<Value, Update, Result>, scope?: Scope) { if ('scope' in atom) { console.warn( 'atom.scope is deprecated. Please do useAtom(atom, scope) instead.' ) scope = (atom as { scope: Scope }).scope } return [ useAtomValue(atom, scope), // We do wrong type assertion here, which results in throwing an error. useSetAtom(atom as WritableAtom<Value, Update, Result>, scope), ]}