usehooks icon indicating copy to clipboard operation
usehooks copied to clipboard

What's the point of dispatching dispatching the local-storage event in useLocalStorage?

Open rostero1 opened this issue 2 years ago • 0 comments

useLocalStorage will call window.dispatchEvent(new Event("local-storage")). Then there's

  const onStorageChange = React.useEffectEvent((event) => {
    if (event?.key && event.key !== key) {
      return;
    }

    setLocalState(readValue());
  });

  React.useEffect(() => {
    window.addEventListener("storage", onStorageChange);
    window.addEventListener("local-storage", onStorageChange);

    return () => {
      window.removeEventListener("storage", onStorageChange);
      window.removeEventListener("local-storage", onStorageChange);
    };
  }, []);

But event?.key will always be undefined for new Event("local-storage")

rostero1 avatar Jun 01 '23 12:06 rostero1