UNPKG

@darwish/hooks-core

Version:

19 lines (18 loc) 585 B
import { freeze, produce } from 'immer'; import { useCallback, useState } from 'react'; export default function useImmer(initialValue) { var _a = useState(function () { return freeze(typeof initialValue === 'function' ? initialValue() : initialValue, true); }), val = _a[0], updateValue = _a[1]; return [ val, useCallback(function (updater) { if (typeof updater === 'function') updateValue(produce(updater)); else updateValue(freeze(updater)); }, []), ]; }