re: A Complete Beginner's Guide to React VIEW POST

FULL DISCUSSION
 

Great article!
I've noticed a small thing - today's approach with methods is not to write

this.metod = this.method.bind(this)

in constructor function, but simply define methods with arrow functions like that:

class Comment extends React.Component {

  handleChange = (event) => {
    this.setState({
      characterCount: event.target.value.length
    })
  }

  render() {}
}

This way our "this" is the method's parent Class.
Please correct me if I'm wrong :)

 

You can do that -- its a Babel feature, so its not implemented in JS, it also has some performance issues. I like the syntax, but its still perfectly valid to bind in the constructor, which IMO is easier to explain.

 

Ok, thank You for an answer :)
I will try to check more about performance (I didn't have problems with that), so thanks for suggestion!

code of conduct - report abuse