Friday, September 27, 2019

localStorage and sessionStorage To Store Data

localStorage and sessionStorage To Store Data
....................................................................................................................................................................................................................................
LocalStorage

import React, { Component } from 'react'
class App extends Component {

  setData() {
    let obj = { name: 'Sapan', password: 123333, email: 'my@gmail.com' }
    localStorage.setItem('mysetData', JSON.stringify(obj))
  }
  
  getData() {
    let data = localStorage.getItem('mysetData')
    data = JSON.parse(data)
    console.log(data)
   console.log(data.email)
  }

  render() {
    return (
      <div>
        <h1>Hello</h1>
        <button onClick={() => this.setData()}>setData</button>
        <button onClick={() => this.getData()}>getData</button>
      </div>
    )
  }
}

export default App

................................................................................................................................................................................................................................
SessionStorage

import React, { Component } from 'react'
class App extends Component {

  setData() {
    let obj = { name: 'Sapan', password: 123333, email: 'my@gmail.com' }
    sessionStorage.setItem('mysetDatasession', JSON.stringify(obj))
  }

  getData() {
    let data = sessionStorage.getItem('mysetDatasession')
    data = JSON.parse(data)
    console.log(data)
    console.log(data.email)
    console.log(data.name)
  }

  render() {
    return (
      <div>
        <h1>Hello</h1>
        <button onClick={() => this.setData()}>setData</button>
        <button onClick={() => this.getData()}>getData</button>
      </div>
    )
  }
}

export default App







No comments:

Post a Comment