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 :: larger text console javascript 
Javascript :: how to access vuex state properties with getters in nuxt vuex 
Javascript :: javascript get bounding rect 
Javascript :: javascript array remove element 
Javascript :: ubuntu nodejs update 
Javascript :: insertone mongoose 
Javascript :: check if reCaptcha is sucess 
Javascript :: adonisjs livereload 
Javascript :: nodejs remove unsafe string 
Javascript :: how to push the get variables without page reloading in Jquery 
Javascript :: python phantomjs current url 
Javascript :: how to round double value in js 
Javascript :: Moment js get first and last day of current month 
Javascript :: setrequestheader authorization bearer 
Javascript :: localstorage setitem javascript 
Javascript :: yarn react-bootstrap 
Javascript :: js find key by value in object 
Javascript :: is java and javascript a good combo 
Javascript :: unpacking array javascript 
Javascript :: how to convert char to number in js 
Javascript :: this.$set 
Javascript :: truthy or falsy value javascript 
Javascript :: how to check if iframe is loaded 
Javascript :: JS get number of classes in html 
Javascript :: react-native array.filter by index arrow function 
Javascript :: update node js version ubuntu 
Javascript :: model schema mongoose 
Javascript :: set cookie javascript 
Javascript :: regex validate double with 2 decimals 
Javascript :: js regex password 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =