UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

45 lines (44 loc) 1.33 kB
import { useRef } from 'react'; import { useRerender } from '..'; var proto = Map.prototype; /** * Tracks the state of a `Map`. * * @param entries Initial entries iterator for underlying `Map` constructor. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function useMap(entries) { var mapRef = useRef(); var rerender = useRerender(); if (!mapRef.current) { var map_1 = new Map(entries); mapRef.current = map_1; map_1.set = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } proto.set.apply(map_1, args); rerender(); return map_1; }; map_1.clear = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } proto.clear.apply(map_1, args); rerender(); }; map_1.delete = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var res = proto.delete.apply(map_1, args); rerender(); return res; }; } return mapRef.current; }