Storing values in sessionStorage in ReactJs

9.3k views Asked by At

I have stored all the values in state called data as below:

loadValues = () => {
    this.setState({
      data: [`{"Price:" "${this.state.price}","Title": "${this.state.title}","MenuId": "${this.state.code}","Quantity": "${this.state.counter}"}`],
    })
  }

Now i need help in storing these values in sessionStorage and print it in console.(reactJs). Thanks in advance.

3

There are 3 answers

0
Omid Nikrah On BEST ANSWER

You can only do like the following:

loadValues = () => {
  this.setState({
    data: [`{"Price:" "${this.state.price}","Title": "${this.state.title}","MenuId": "${this.state.code}","Quantity": "${this.state.counter}"}`],
  }, () => {
    sessionStorage.setItem('data', this.state.data)
  })
}
0
Prafful Singh On

u can use sessionStorage.setItem() to store values and sessionStorage.getItem() to get values

0
jsDevia On

It's not a question about reactJs. It's just javascript. use this

sessionStorage.setItem('key', 'value');