Nam Mai

I am software engineer. Currently doing more in front-end, focused in React and C#.

ES6 syntax

01 Feb 2018 » react

How different is React’s ES6 syntax when compared to ES5?

  1. Require vs Import
var React = require('react'); //es5
import React from 'react'; //es6
  1. Export vs Exports
module.exports = Component; //es5
export default Component; //es6
  1. Component and Function
var Demo = React.createClass({
  render: function() {
    return <h1>Hi</h1>;
  }
}); //es5

class Demo extends React.Component {
  render() {
    return <h1>Hi</h1>;
  }
} // es6
  1. Props
var App = React.createClass({
  propTypes: { name: React.PropTypes.string },
  render: function() {
    return <h1>Hi {this.props.name}</h1>;
  }
}); //es5

class Demo extends React.Component {
  render() {
    return <h1>Hi {this.props.name}</h1>;
  }
} // es6
  1. State
var App = React.createClass({
  getInitialState: function() {
    return { name: 'world' };
  },
  render: function() {
    return <h1>Hi {this.state.name}</h1>;
  }
}); //es5

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'world'
    };
  }

  render() {
    return <h1>Hi {this.state.name}</h1>;
  }
} // es6