Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react localstorage

// setter
localStorage.setItem('myData', data);
// getter
localStorage.getItem('myData');
// remove
localStorage.removeItem('myData');
// remove all
localStorage.clear();
Comment

React localStorage

const useLocalStorage = (keyName, defaultValue) => {
  const [storedValue, setStoredValue] = React.useState(() => {
    try {
      const value = window.localStorage.getItem(keyName);

      if (value) {
        return JSON.parse(value);
      } else {
        window.localStorage.setItem(keyName, JSON.stringify(defaultValue));
        return defaultValue;
      }
    } catch (err) {
      return defaultValue;
    }
  });

  const setValue = newValue => {
    try {
      window.localStorage.setItem(keyName, JSON.stringify(newValue));
    } catch (err) {}
    setStoredValue(newValue);
  };

  return [storedValue, setValue];
};

Example
const MyApp = () => {
  const [name, setName] = useLocalStorage('name', 'John');

  return <input value={name} onChange={e => setName(e.target.value)} />;
};

ReactDOM.render(<MyApp />, document.getElementById('root'));
Comment

local storage react

import { useState, useEffect } from "react";

function getStorageValue(key, defaultValue) {
  // getting stored value
  const saved = localStorage.getItem(key);
  const initial = JSON.parse(saved);
  return initial || defaultValue;
}

export const useLocalStorage = (key, defaultValue) => {
  const [value, setValue] = useState(() => {
    return getStorageValue(key, defaultValue);
  });

  useEffect(() => {
    // storing input name
    localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);

  return [value, setValue];
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: react route path exact 
Javascript :: javascriot function 
Javascript :: what is xhr 
Javascript :: request get response node js 
Javascript :: onclick send to email javascript 
Javascript :: lodash compare array without order 
Javascript :: promise with timeout js 
Javascript :: add id to Array of Object 
Javascript :: javascript array contains 
Javascript :: printing in a single line in javascript 
Javascript :: javascript date get next 15 minutes 
Javascript :: learn nestjs 
Javascript :: how to use a regex expression in kibana query 
Javascript :: using apis in javascript 
Javascript :: how to detect click outside div 
Javascript :: nodejs end process 
Javascript :: react detect page width 
Javascript :: convert namednodemap to object 
Javascript :: react html parser 
Javascript :: Comparing and Filtering two arrays 
Javascript :: js add html element 
Javascript :: get selected text input javascript 
Javascript :: js detect if content editable div is empty 
Javascript :: javascript como recorrer un array multidimensional 
Javascript :: setstate array 
Javascript :: ssg full form in nextjs 
Javascript :: prevstate in react 
Javascript :: object check null or empty 
Javascript :: MaterialStateProperty width 
Javascript :: clearing setinterval 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =