ES 2009 to ES 2015

ES 2009 = ES5
ES 2015 = ES6


require vs import

//  ES5 
var React = require('react');

// ES6
import React from 'react';

export vs exports

//  ES5 
module.exports = Component;

// ES6
export [default] Component;

class vs React.createClass

//  ES5 
var MyComponent = React.createClass({
  render: function() {
    return (
      <h3>Better</h3>;
    );
  }
});

// ES6
class MyComponent extends React.Component {
  render() {
    return (
      <h3>Better</h3>
    );
  }
}

props

//  ES5 
var MyComponent = React.createClass({
  propTypes: {name: React.PropTypes.string },
  render: function() {
    return (
      <h3>{this.props.name}</h3>

// ES6
class MyComponent extends React.Component {
  render() {
    return (
      <h3>{this.props.name}</h3>

state

//  ES5 
var MyComponent = React.createClass({
  getInitialState: function() {
    return {name: 'better'};
  },
  render: function() {
    return (
      <h3>{this.state.name}</h3>

// ES6
class MyComponent extends React.Component {
  constructor() {
    super();
    this.state = {name: 'better'};
  }
  render() {
    return (
      <h3>{this.state.name}</h3>


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext