Search code examples
reactjsstatesetstate

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?


Solution

  • 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.