1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var react_1 = require("react");
|
---|
4 | var util_1 = require("./misc/util");
|
---|
5 | var useLocalStorage = function (key, initialValue, options) {
|
---|
6 | if (!util_1.isBrowser) {
|
---|
7 | return [initialValue, util_1.noop, util_1.noop];
|
---|
8 | }
|
---|
9 | if (!key) {
|
---|
10 | throw new Error('useLocalStorage key may not be falsy');
|
---|
11 | }
|
---|
12 | var deserializer = options
|
---|
13 | ? options.raw
|
---|
14 | ? function (value) { return value; }
|
---|
15 | : options.deserializer
|
---|
16 | : JSON.parse;
|
---|
17 | // eslint-disable-next-line react-hooks/rules-of-hooks
|
---|
18 | var initializer = react_1.useRef(function (key) {
|
---|
19 | try {
|
---|
20 | var serializer = options ? (options.raw ? String : options.serializer) : JSON.stringify;
|
---|
21 | var localStorageValue = localStorage.getItem(key);
|
---|
22 | if (localStorageValue !== null) {
|
---|
23 | return deserializer(localStorageValue);
|
---|
24 | }
|
---|
25 | else {
|
---|
26 | initialValue && localStorage.setItem(key, serializer(initialValue));
|
---|
27 | return initialValue;
|
---|
28 | }
|
---|
29 | }
|
---|
30 | catch (_a) {
|
---|
31 | // If user is in private mode or has storage restriction
|
---|
32 | // localStorage can throw. JSON.parse and JSON.stringify
|
---|
33 | // can throw, too.
|
---|
34 | return initialValue;
|
---|
35 | }
|
---|
36 | });
|
---|
37 | // eslint-disable-next-line react-hooks/rules-of-hooks
|
---|
38 | var _a = react_1.useState(function () { return initializer.current(key); }), state = _a[0], setState = _a[1];
|
---|
39 | // eslint-disable-next-line react-hooks/rules-of-hooks
|
---|
40 | react_1.useLayoutEffect(function () { return setState(initializer.current(key)); }, [key]);
|
---|
41 | // eslint-disable-next-line react-hooks/rules-of-hooks
|
---|
42 | var set = react_1.useCallback(function (valOrFunc) {
|
---|
43 | try {
|
---|
44 | var newState = typeof valOrFunc === 'function' ? valOrFunc(state) : valOrFunc;
|
---|
45 | if (typeof newState === 'undefined')
|
---|
46 | return;
|
---|
47 | var value = void 0;
|
---|
48 | if (options)
|
---|
49 | if (options.raw)
|
---|
50 | if (typeof newState === 'string')
|
---|
51 | value = newState;
|
---|
52 | else
|
---|
53 | value = JSON.stringify(newState);
|
---|
54 | else if (options.serializer)
|
---|
55 | value = options.serializer(newState);
|
---|
56 | else
|
---|
57 | value = JSON.stringify(newState);
|
---|
58 | else
|
---|
59 | value = JSON.stringify(newState);
|
---|
60 | localStorage.setItem(key, value);
|
---|
61 | setState(deserializer(value));
|
---|
62 | }
|
---|
63 | catch (_a) {
|
---|
64 | // If user is in private mode or has storage restriction
|
---|
65 | // localStorage can throw. Also JSON.stringify can throw.
|
---|
66 | }
|
---|
67 | }, [key, setState]);
|
---|
68 | // eslint-disable-next-line react-hooks/rules-of-hooks
|
---|
69 | var remove = react_1.useCallback(function () {
|
---|
70 | try {
|
---|
71 | localStorage.removeItem(key);
|
---|
72 | setState(undefined);
|
---|
73 | }
|
---|
74 | catch (_a) {
|
---|
75 | // If user is in private mode or has storage restriction
|
---|
76 | // localStorage can throw.
|
---|
77 | }
|
---|
78 | }, [key, setState]);
|
---|
79 | return [state, set, remove];
|
---|
80 | };
|
---|
81 | exports.default = useLocalStorage;
|
---|