UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

16 lines (15 loc) • 559 B
'use client'; import { useCallback } from 'react'; import { useStore } from './Provider.js'; export function useSetAtom(atom, options) { const store = useStore(options); const setAtom = useCallback((...args) => { if (process.env.NODE_ENV !== 'production' && !('write' in atom)) { // useAtom can pass non writable atom with wrong type assertion, // so we should check here. throw new Error('not writable atom'); } return store.set(atom, ...args); }, [store, atom]); return setAtom; }