UNPKG

react-use

Version:
25 lines (24 loc) 993 B
import { __assign, __rest } from "tslib"; import { useState, useMemo, useCallback } from 'react'; var useMap = function (initialMap) { if (initialMap === void 0) { initialMap = {}; } var _a = useState(initialMap), map = _a[0], set = _a[1]; var stableActions = useMemo(function () { return ({ set: function (key, entry) { set(function (prevMap) { var _a; return (__assign(__assign({}, prevMap), (_a = {}, _a[key] = entry, _a))); }); }, remove: function (key) { set(function (prevMap) { var _a = key, omit = prevMap[_a], rest = __rest(prevMap, [typeof _a === "symbol" ? _a : _a + ""]); return rest; }); }, reset: function () { return set(initialMap); }, }); }, [set]); var utils = __assign({ get: useCallback(function (key) { return map[key]; }, [map]) }, stableActions); return [map, utils]; }; export default useMap;