这里,handler函数中的props名称与state中的名称相同,都是“phone”
state = {
phone: '',
}
.
<TextInput
onChangeText={this.handlePhoneChange}
/>
.
handlePhoneChange = phone => {
this.setState({phone})
}
不应该是this.setState({phone:phone})
吗?这两个陈述是同义词还是我遗漏了什么?
最佳答案
New in JavaScript with ES6/ES2015, if you want to define an object who's keys have the same name as the variables passed-in as properties, you can use the shorthand and simply pass the key name.
这是 , 的简短语法
{phone : phone}
如果键名称与变量名称相同,您可以使用它,如下所示:
{phone}
<强> ALSO DO READ
关于reactjs - react native : Assigning value with setState(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61812553/