blob: 8bc4401feecbf848d3eb82ac92b492d1da946422 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import { useEffect, useState } from 'react';
export const savePersistedState = (key, value) => {
window.localStorage.setItem(key, JSON.stringify(value));
};
export default (key, defaultValue) => {
const [value, setValue] = useState(() => {
const stickyValue = window.localStorage.getItem(key);
return stickyValue ? JSON.parse(stickyValue) : defaultValue;
});
useEffect(() => {
savePersistedState(key, value);
}, [key, value]);
return [value, setValue];
};
|