DEV Community

Discussion on: A Complete Beginner's Guide to React

Collapse
 
rbatsenko profile image
Roman Batsenko

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

this.metod = this.method.bind(this)
Enter fullscreen mode Exit fullscreen mode

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() {}
}
Enter fullscreen mode Exit fullscreen mode

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

Collapse
 
aspittel profile image
Ali Spittel

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.

Collapse
 
rbatsenko profile image
Roman Batsenko

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!