coding180 icon
Coding180

What is the difference between React's ES6 syntax and ES5 syntax?

Home > Interviews > Javascript


robort - 2022-10-05 06:10:14


React's ES6 syntax differs from ES5 syntax in the following ways.

require vs. Import

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

// ES6  
import React from 'react';

Js

exports vs. export

// ES5  
module.exports = Component;  

// ES6  
export default Component;

Js

Components and Functions

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

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

Js

props

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

// ES6  
class App extends React.Component {  
    render() {  
        return(  
          <h3>Hello, {this.props.name}!</h3>  
        );  
    }  
}

Js

state:

var App = React.createClass({  
    getInitialState: function() {  
        return { name: 'world' };  
    },  
    render: function() {  
        return(  
          <h3>Hello, {this.state.name}!</h3>  
        );  
    }  
});  

// ES6  
class App extends React.Component {  
    constructor() {  
        super();  
        this.state = { name: 'world' };  
    }  
    render() {  
        return(  
          <h3>Hello, {this.state.name}!</h3>  
        );  
    }  
}

 


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel