简体   繁体   English

在ReactJs中的sessionStorage中存储值

[英]Storing values in sessionStorage in ReactJs

I have stored all the values in state called data as below: 我将所有值存储在状态为data的状态中,如下所示:

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). 现在,我需要帮助将这些值存储在sessionStorage中并在控制台中将其打印出来。(reactJs)。 Thanks in advance. 提前致谢。

您可以使用sessionStorage.setItem()存储值,并使用sessionStorage.getItem()获取值

It's not a question about reactJs. 这不是关于reactJs的问题。 It's just javascript. 这只是javascript。 use this 用这个

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

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)
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM