usehooks
usehooks copied to clipboard
What's the point of dispatching dispatching the local-storage event in useLocalStorage?
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")