We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5e4acfc commit 464fef6Copy full SHA for 464fef6
1 file changed
src/hooks/useSessionStorage.js
@@ -0,0 +1,26 @@
1
+import { useState } from 'react';
2
+
3
+const useSessionStorage = (key, initialValue) => {
4
+ const [storedValue, setStoredValue] = useState(() => {
5
+ try {
6
+ const item = sessionStorage.getItem(key);
7
+ return item ? JSON.parse(item) : initialValue;
8
+ } catch (error) {
9
+ console.error(error);
10
+ return initialValue;
11
+ }
12
+ });
13
14
+ const setValue = (value) => {
15
16
+ setStoredValue(value);
17
+ sessionStorage.setItem(key, JSON.stringify(value));
18
19
20
21
+ };
22
23
+ return [storedValue, setValue];
24
+};
25
26
+export default useSessionStorage;
0 commit comments