@lesnoypudge/utils-react
Version:
lesnoypudge's utils-react
36 lines (35 loc) • 979 B
JavaScript
import { useFunction } from "../useFunction/useFunction.js";
import { useState } from "react";
const useSet = (defaultValue) => {
const [value, setValue] = useState(() => new Set(defaultValue));
const _add = useFunction((...args) => {
const newValue = new Set(value);
Set.prototype.add.apply(newValue, args);
setValue(newValue);
return newValue;
});
const _delete = useFunction((...args) => {
const newValue = new Set(value);
const res = Set.prototype.delete.apply(newValue, args);
setValue(newValue);
return res;
});
const _clear = useFunction(() => {
setValue(/* @__PURE__ */ new Set());
});
return {
size: value.size,
add: _add,
clear: _clear,
delete: _delete,
entries: value.entries.bind(value),
forEach: value.forEach.bind(value),
has: value.has.bind(value),
keys: value.keys.bind(value),
values: value.values.bind(value)
};
};
export {
useSet
};
//# sourceMappingURL=useSet.js.map