1

EDIT: This question isn't a duplicate as far as I can see, because the issue isn't whether to use arrow functions or not, it is whether or not using a constructor, even though not required when using arrow functions, is more performant, or cleaner syntax, or best practice. I am confused as to when to use the constructor and when not to, as I have never needed to use one, nor was I taught that it was ever necessary. This is the issue I am trying to clarify, and I have not been able to after significant effort.

I have googled around quite a bit, and haven't quite been able to find the answer to my question. When I learned React recently, I was taught to write a component like this, with no constructor:

class App extends React.Component {
  state = {
      color: "green"
  }
  changeColor = () => {
    if (this.state.color === "green"){
      this.setState({color: "yellow"})
    } else {
      this.setState({color: "green"})
  }

  render() {
    return(
      <div>
        <h1 style={{color: this.state.color}}>What color am I?</h1>
        <button className='btn btn-default' onClick={this.changeColor}>Change Color</button>
      </div>
    )
  }
}

Where the arrow function binds the context of "this". This code runs just fine, but every tutorial I see, every video I watch, every bit of other people's code I see has it written like this:

class App extends React.Component {
  constructor(props){
    super(props)
    this.state={
      color: "green"
    }
    this.changeColor = this.changeColor.bind(this);
  }
  changeColor(){
    if (this.state.color === "green"){
      this.setState({color: "yellow"})
    } else {
      this.setState({color: "green"})
  }

  render() {
    return(
      <div>
        <h1 style={{color: this.state.color}}>What color am I?</h1>
        <button className='btn btn-default' onClick={this.changeColor}>Change Color</button>
      </div>
    )
  }
}

What I am curious about is whether there is a benefit to using the constructor, or is the first example a better way to do it? Are there pitfalls I am not aware of with doing this the way I am doing? Any help would be greatly appreciated.

Marco Principio
  • 475
  • 3
  • 9
  • 3
    The first syntax is not really official yet, so it needs Babel. – elclanrs Feb 22 '18 at 23:26
  • I see, so is it not good practice to use the first syntax? Would I be better off writing my code in a more conventional way? – Marco Principio Feb 22 '18 at 23:41
  • The proposal ist currently stage 3 which is the final stage before being included in the javascript language standard. `create-react-app` enables this feature by default. Also it's already widely used all over the place. It don't see any problem with that although there is a minimal likelihood it will for some reason not be included in the next standard. I use it every day and it's just much cleaner to write and often saves you the constructor at all. – trixn Feb 22 '18 at 23:47
  • *I am confused as to when to use the constructor and when not to* - [This answer to the second duplicate question](https://stackoverflow.com/a/45898085) (of the marked duplicate) seems to address your exact question. – NightOwl888 Feb 23 '18 at 05:04

0 Answers0