简体   繁体   English

React Native:使用 setState() 赋值

[英]React Native: Assigning value with setState()

Here, props name in the handler function is the same as the name in state, both are `phone'这里,处理程序 function 中的道具名称与 state 中的名称相同,都是“电话”

state = {
    phone: '',
  }

. .

<TextInput
          onChangeText={this.handlePhoneChange}
        />

. .

handlePhoneChange = phone => {
    this.setState({phone})
  }

shouldn't it be this.setState({phone:phone}) ?不应该是this.setState({phone:phone})吗? are the two statements synonymous or am I missing something?这两个陈述是同义词还是我错过了什么?

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. JavaScript 与 ES6/ES2015 中的新增功能,如果您想定义一个 object 的键与作为属性传入的变量具有相同的名称,您可以使用简写并简单地传递键名。

That is short syntax for,这是简短的语法,

{phone : phone}

if key name is same as variable name you can use it like:如果键名与变量名相同,您可以像这样使用它:

{phone}

ALSO DO READ 也请阅读

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

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