aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common/usePersistedState.js
blob: 0a7a990efb6d13d50ed895d925798d0836f72cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { useEffect, useState } from 'react';

export default (key, defaultValue) => {
  const [value, setValue] = useState(() => {
    const stickyValue = window.localStorage.getItem(key);
    return stickyValue ? JSON.parse(stickyValue) : defaultValue;
  });

  useEffect(() => {
    window.localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);

  return [value, setValue];
};