Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

reactive localstorage in react


function useStorageWatcher(key) {
  const [val, setVal] = useState(localStorage.get(key));

  useEffect(() => {
    const handler = () => val !== localStorage.get(key) && setVal(localStorage.get(key));
    window.addEventListener("storage", handle);
    () => window.removeEventListener("storage", handler);
  }, []);

  useEffect(() => {
    setVal(localStorage.get(key));
  }, [key]);

  return val;

}


Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript filter array of objects by id 
Javascript :: js number validation 
Javascript :: generate random email javascript 
Javascript :: unique values from array of objects 
Javascript :: how to generate 6 random alphanumerals in js 
Javascript :: node js random number generator 
Javascript :: enzyme change input value 
Javascript :: how to hover the mouse on an element cypress mouseover 
Javascript :: enable native bracket matching vs cide 
Javascript :: hide status bar react native 
Javascript :: js input text set value 
Javascript :: javascript remove last character 
Javascript :: javascript loop thrugh array 
Javascript :: javascript HOW set delay 
Javascript :: FailedToParse: Password must be URL Encoded for mongodb: 
Javascript :: javascript Count the occurrences of a value in an array 
Javascript :: loop json 
Javascript :: how to update node modules 
Javascript :: laravel 8 include javascript in blade 
Javascript :: random color in javascript 
Javascript :: chart js rotating the x axis labels 
Javascript :: jvascript number to column letter 
Javascript :: validar solo letras js 
Javascript :: how to output only a certain length of a string in javascript 
Javascript :: joi email validation regex 
Javascript :: how to store words in an array in javascript 
Javascript :: react native object is empty 
Javascript :: javascript restart video 
Javascript :: npm install router dom 
Javascript :: loopback find or create 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =