Class vs React.createClass vs stateless

  • If you have internal state and/or refs, prefer class extends React.Component over React.createClass. eslint: react/prefer-es6-class react/prefer-stateless-function

    1. // bad
    2. const Listing = React.createClass({
    3. // ...
    4. render() {
    5. return <div>{this.state.hello}</div>;
    6. }
    7. });
    8. // good
    9. class Listing extends React.Component {
    10. // ...
    11. render() {
    12. return <div>{this.state.hello}</div>;
    13. }
    14. }

    And if you don’t have state or refs, prefer normal functions (not arrow functions) over classes:

    1. // bad
    2. class Listing extends React.Component {
    3. render() {
    4. return <div>{this.props.hello}</div>;
    5. }
    6. }
    7. // bad (relying on function name inference is discouraged)
    8. const Listing = ({ hello }) => (
    9. <div>{hello}</div>
    10. );
    11. // good
    12. function Listing({ hello }) {
    13. return <div>{hello}</div>;
    14. }