React ES6
What is ES6?
React itself is not a programming language but a JavaScript library used for building user interfaces. However, React code is typically written using ECMAScript 6 (ES6) or later, which is a standardized version of JavaScript. ES6 introduces several features that enhance the readability and functionality of JavaScript code.
Here are some common ES6 features used in React development:
1.Arrow Functions:
// ES5
function myFunction() {
// function body
}
// ES6
const myFunction = () => {
// function body
};
2.Classes:
// ES5
var MyClass = React.createClass({
render: function() {
return <div>Hello, World! </div>;
}
});
// ES6
class MyClass extends React.Component {
render() {
return <div>Hello, World! </div>;
}
}
3.Destructuring Assignment:
// ES5
var obj = { x: 1, y: 2 };
var x = obj.x;
var y = obj.y;
// ES6
const { x, y } = { x: 1, y: 2 };
4.Template Literals:
// ES5
var name = "John";
var greeting = "Hello, " + name + "!";
// ES6
const name = "John";
const greeting = `Hello, ${name}!`;
5.Spread Operator:
// ES5
var arr1 = [1, 2, 3];
var arr2 = arr1.concat([4, 5, 6]);
// ES6
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
6.Import and Export:
// Importing in ES6
import React from 'react';
import MyComponent from './MyComponent';
// Exporting in ES6
export default class MyComponent extends React.Component {
// component code
}