• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📘The missing manual of upgrading ES5 React to ES6+ ✨

ES6 and beyond is very popular.

JS is also unlike any other language in that the experimentation occurs on massive scale thanks to transpilers. Never happened before. -- Dan Abramov

React is a declarative, efficient, and flexible JavaScript library for building user interfaces.

In the earlier days of React development, many programmes were written in ES5. Howerver, as ES6 and beyond is more widely accepted now and with the help of transpiler tools like Babel, we can gradually retire some ES5 code and adopt some new JavaScript features in React development.

Here is a check list to help you translate your ES5 code to ES next in React. You are welcome to contribute with more items provided below.

Quick links

  1. require => import
  2. createClass => extends React.Component
  3. module.exports => export default
  4. name function() => name()
  5. getDefaultProps and propTypes
  6. getInitialState
  7. destructuring & spread attributes
  8. use arrow functions

require => import

ES5

//ES5
var React = require("react");  
var PropTypes = React.PropTypes;

ES6

import React, { Component, PropTypes } from 'react';

⬆ back to top

createClass => extends React.Component

ES5

var Mycomponent = React.createClass({
  render: function() {
    return (
      <div>ES5</div>
    );
  },
});

ES6

class Mycomponent extends React.Component {
  render() {
    return (
      <div>ES6</div>
    );
  }
}

⬆ back to top

module.exports => export default

ES5

var Mycomponent = React.createClass({
  render: function() {
    return (
      <div>ES5</div>
    );
  },
});

module.exports = Mycomponent;

ES6

export default class Mycomponent extends React.Component {
  render() {
    return (
      <div>ES6</div>
    );
  }
}

⬆ back to top

name function() => name()

ES5

var Mycomponent = React.createClass({
  componentWillMount: function(){

  },
  render: function() {
    return (
      <div>ES5</div>
    );
  },
});

module.exports = Mycomponent;

ES6

export default class Mycomponent extends React.Component {
  componentWillMount() {

  }
  render() {
    return (
      <div>ES6</div>
    );
  }
}

⬆ back to top

getDefaultProps and propTypes

ES5

var Video = React.createClass({
    getDefaultProps: function() {
        return {
            autoPlay: false,
            maxLoops: 10,
        };
    },
    propTypes: {
        autoPlay: React.PropTypes.bool.isRequired,
        maxLoops: React.PropTypes.number.isRequired,
        posterFrameSrc: React.PropTypes.string.isRequired,
        videoSrc: React.PropTypes.string.isRequired,
    },
    render: function() {
        return (
            <View />
        );
    },
});

ES6

class Video extends React.Component {
  render() {
      return (
          <View />
      );
  }
}
Video.defaultProps = {
    autoPlay: false,
    maxLoops: 10,
};
Video.propTypes = {
    autoPlay: React.PropTypes.bool.isRequired,
    maxLoops: React.PropTypes.number.isRequired,
    posterFrameSrc: React.PropTypes.string.isRequired,
    videoSrc: React.PropTypes.string.isRequired,
};

ES future proposals:

class Video extends React.Component {
  static defaultProps = {
    autoPlay: false,
    maxLoops: 10,
  }
  static propTypes = {
    autoPlay: React.PropTypes.bool.isRequired,
    maxLoops: React.PropTypes.number.isRequired,
    posterFrameSrc: React.PropTypes.string.isRequired,
    videoSrc: React.PropTypes.string.isRequired,
  }
  state = {
    loopsRemaining: this.props.maxLoops,
  }
}

⬆ back to top

getInitialState

ES5

var Header = React.createClass({
  getInitialState: function() {
    return {
      title: this.props.title
    };
  },
});

ES6

export default class Header extends Component {
  constructor(props) {
    super(props);
    this.state = {
      title: props.title
    };
  }
}

ES future proposals:

export default class Header extends Component {
  state = {
    title: this.props.title
  };
    
  // followed by constructor...
}

⬆ back to top

destructuring & spread attributes

class AutoloadingPostsGrid extends React.Component {
  render() {
    var {
      className,
      ...others,  // contains all properties of this.props except for className
    } = this.props;
    return (
      <div className={className}>
        <PostsGrid {...others} />
        <button onClick={this.handleLoadMoreClick}>Load more</button>
      </div>
    );
  }
}

⬆ back to top

use arrow functions

Stateless component:

function App() {
  return (
    <div>
      <MyComponent />
    </div>
  );
}

Using Arrow function:

const App = () => (
  <div>
    <MyComponent />
  </div>
);

Using Arrow funtion with Destructuring function arguments:

const App = ({className, ...rest}) => (
  <div className={classnames(className)} {...rest}>
    <MyComponent />
  </div>
);

⬆ back to top

Reference

Roadmap

  • Include all ES5 to ES6+ instructions for React
  • We may Implement ESlint plugin like ESlint-Plugin-React-5-to-6
  • We may Include instructions for configuring Babel with different stages

License

MIT

More Repositories

1

react-hover

React hover --- make hover easy http://cht8687.github.io/react-hover/example/
JavaScript
107
star
2

react-listview-sticky-header

react listview with sticky section header
JavaScript
80
star
3

TSA

Using React-Native and Redux to create the TSA $1.4 million iOS and Android app
Objective-C
68
star
4

mingify

Mingify anything!
JavaScript
42
star
5

react-expandable-listview

React expandable components, assist you render expandable objects or React components
JavaScript
37
star
6

react-text-collapse

React text collapse/expand with React-Motion...
JavaScript
19
star
7

angular-redux-starter-kit

Angular + Angular-Redux (@angular-redux/store) + Bootstrap + Font-awesome starter. Simple yet extendable.
TypeScript
6
star
8

react-accordion

React accordion component --- Convert a pair of headers and content panels into an accordion using React, React-motion
JavaScript
5
star
9

code-warehouse

Code Snippets, algorithms, practice...
JavaScript
3
star
10

string-remove

Remove any unwanted stuffs in string
JavaScript
2
star
11

diff-at

Return the index of first difference occurring between given string, number or array
JavaScript
2
star
12

react-webpack-starter-kit

React Webpack Starter Kit--- no Flux, Redux, Reflux, chose your own framework...
JavaScript
2
star
13

year-of-dog

Check if the year is year of dog in Chinese Zodiac
JavaScript
2
star
14

HL-minigames

react-pixi mini-games starter kit
JavaScript
1
star
15

blog

my blog site
HTML
1
star
16

cht8687

1
star
17

cht8687.github.io

JavaScript
1
star
18

react-timeline-d3

A event based d3 timeline built for React
1
star
19

fp-ts-string-uniq

string uniq util function
TypeScript
1
star
20

awards

repo to host a collection of personal awards
1
star
21

nsw-vaccine-tracker

small termal program to track available slots
JavaScript
1
star
22

react-busy

Show busy/loading indicators whenever you like.
JavaScript
1
star
23

react-tooltip

react tooltip
JavaScript
1
star
24

covid-19-global

Global site for tracking covid-19
JavaScript
1
star