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