useStickyState.js

import { useState, useEffect } from 'react';

// https://joshwcomeau.com/react/persisting-react-state-in-localstorage/
export default function useStickyState(key, defaultValue = null) {
  const [value, setValue] = useState(() => {
    const stickyValue = window.localStorage.getItem(key);
    return stickyValue ? JSON.parse(stickyValue) : defaultValue;
  });
  // update localstorage whenever `value` changes
  useEffect(() => {
    if (value) {
      window.localStorage.setItem(key, JSON.stringify(value));
    }
  }, [key, value]);
  return [value, setValue];
}