npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

React-ES5-To-ES6-Checklist

v1.0.0

Published

The missing manual of upgrading ES5 React project to ES6+

Downloads

4

Readme

ECMAScript2015(ES6) is the latest version of the ECMAScript standard.

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

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

Here is a check list to help you translate your ES5 code to newer version 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

require-to-import

ES5

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

ES6

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

⬆ back to top

createClass-to-extends

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-to-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

functions

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,
};

ES7:

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
      };
    }
}

ES7

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

⬆ back to top

Destructuring-and-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

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