Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react - setState in short form

Can you write

    let firstNameValid = this.state.firstNameValid;

    firstNameValid = value.length >= 1; 

    this.setState({ firstNameValid, firstNameValid,}) 

as short form syntax

this.setState({ firstNameValid})  

I have tried the above code, it seems to work fine. Just wonder if it will have any side effect?

like image 962
user21 Avatar asked Dec 14 '22 15:12

user21


1 Answers

this.setState({ firstNameValid }) is just sugar for this.setState({ firstNameValid: firstNameValid }). No side effects.

These are called shorthand property names, and you can read more about them here.

like image 191
Tholle Avatar answered Dec 17 '22 22:12

Tholle