Created
May 5, 2020 15:30
-
-
Save anmolsukki/23c757b3240a79fc329f23c23c8158d0 to your computer and use it in GitHub Desktop.
[ ReactJs ]. Object Store in LocalStorage ( https://codesandbox.io/s/object-store-in-localstorage-2kfit?file=/src/App.js )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
class Storage extends Component { | |
setData = () => { | |
let obj = { name: "Anmol", age: "24", email: "[email protected]" }; | |
localStorage.setItem("myData", JSON.stringify(obj)); | |
}; | |
getData = () => { | |
let data = localStorage.getItem("myData"); | |
data = JSON.parse(data); | |
console.log(data.name); | |
}; | |
render() { | |
return ( | |
<div> | |
<button onClick={() => this.setState()}>Set Data</button> | |
<button onClick={() => this.getData()}>Set Data</button> | |
</div> | |
); | |
} | |
} | |
export default Storage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment