React Native:使用 setState() 赋值
React Native: Assigning value with setState()
这里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}
这里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}