Skip to content

Latest commit

 

History

History
27 lines (23 loc) · 585 Bytes

prefer-es6-class.md

File metadata and controls

27 lines (23 loc) · 585 Bytes

ES6 class bodies are more terse than traditional object literals. Methods do not require a function keyword and no commas are needed to separate them. This refactoring looks as such:

Invalid:

var ExampleComponent = React.createClass({
 render: function() { 
  return <div onClick={this._handleClick}>Hello, world.</div>;
 },
 _handleClick: function() {
  console.log(this);
 }
});

Valid:

class ExampleComponent extends React.Component {
 render() { 
  return <div onClick={this._handleClick}>Hello, world.</div>;
 }
 _handleClick() {
  console.log(this);
 }
}