@vertisanpro/flowbite-react
Version:
Non-Official React components built for Flowbite and Tailwind CSS
16 lines (15 loc) • 541 B
JavaScript
'use client';
import { useEffect } from 'react';
/**
* Triggers `onChange` when another browser tab instance mutates the LS value.
*/
export const useWatchLocalStorageValue = ({ key: watchKey, onChange, }) => {
function handleStorageChange({ key, newValue }) {
if (key === watchKey)
onChange(newValue);
}
useEffect(() => {
window.addEventListener('storage', handleStorageChange);
return () => window.removeEventListener('storage', handleStorageChange);
}, []); // eslint-disable-line
};