main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var tslib_1 = require("tslib");
|
---|
4 | var react_1 = require("react");
|
---|
5 | var useSet = function (initialSet) {
|
---|
6 | if (initialSet === void 0) { initialSet = new Set(); }
|
---|
7 | var _a = react_1.useState(initialSet), set = _a[0], setSet = _a[1];
|
---|
8 | var stableActions = react_1.useMemo(function () {
|
---|
9 | var add = function (item) { return setSet(function (prevSet) { return new Set(tslib_1.__spreadArrays(Array.from(prevSet), [item])); }); };
|
---|
10 | var remove = function (item) {
|
---|
11 | return setSet(function (prevSet) { return new Set(Array.from(prevSet).filter(function (i) { return i !== item; })); });
|
---|
12 | };
|
---|
13 | var toggle = function (item) {
|
---|
14 | return setSet(function (prevSet) {
|
---|
15 | return prevSet.has(item)
|
---|
16 | ? new Set(Array.from(prevSet).filter(function (i) { return i !== item; }))
|
---|
17 | : new Set(tslib_1.__spreadArrays(Array.from(prevSet), [item]));
|
---|
18 | });
|
---|
19 | };
|
---|
20 | return { add: add, remove: remove, toggle: toggle, reset: function () { return setSet(initialSet); }, clear: function () { return setSet(new Set()); } };
|
---|
21 | }, [setSet]);
|
---|
22 | var utils = tslib_1.__assign({ has: react_1.useCallback(function (item) { return set.has(item); }, [set]) }, stableActions);
|
---|
23 | return [set, utils];
|
---|
24 | };
|
---|
25 | exports.default = useSet;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.