简体   繁体   English

React Stateful Class 组件:使用“this.setState”更新 state 的属性,不起作用。 没有错误。 State 不变

[英]React Stateful Class Component: using “this.setState” to update properties of the state, not working. No error. State doesn't change

I know there are several questions covering this, but they didn't help me.我知道有几个问题可以解决这个问题,但它们对我没有帮助。 I basically have this:我基本上有这个:

state: {
  location: 'Tahiti',
  price: ['2000'],
  day: {August}
  
}

componentDidMount() {
    const updatedprice = '1800'
    this.setState({price: updatedprice})
}

I've done more or less the same for the other properties, except for price where I tried to set an object as the new state.除了价格我尝试将 object 设置为新的 state 之外,我对其他属性所做的或多或少相同。 This has always worked for me, and is working elsewhere.这一直对我有用,并且正在其他地方工作。 What is going wrong?出了什么问题? Any pointers?任何指针?

All of the component is basically this.所有的组件基本上都是这样的。 I just wanted to play with modifying the state.我只是想修改 state。

PS I did a console.log on when I'm setting the state: this.setState({price: updatedprice}), and it gives undefined, this happens with other properties too. PS我在设置state时做了一个console.log on:this.setState({price: updatedprice}),它给出了未定义的,这也发生在其他属性上。 Although, I have no errors, it's simply that when I console.log the state, it shows zero difference.虽然,我没有错误,只是当我控制台记录 state 时,它显示零差异。

You just have the syntax wrong.你只是语法错误。

this.setState({price: updatedprice});

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

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