React Native: Assigning value with setState()

Viewed 235

Here, props name in the handler function is the same as the name in state, both are `phone'

state = {
    phone: '',
  }

.

<TextInput
          onChangeText={this.handlePhoneChange}
        />

.

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

shouldn't it be this.setState({phone:phone})? are the two statements synonymous or am I missing something?

1 Answers

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.

That is short syntax for ,

{phone : phone}

if key name is same as variable name you can use it like :

{phone}

ALSO DO READ

Related