Reactjs is the best frontend library ever created. It is made by Facebook to perform several tasks in the frontend itself. ES6 is the standardization of javascript for making code more readable and more accessible.
If we don't use ES6 in react, there is an alternative to perform. We use create-react-class instead of ES6. Let's see some variations between ES6 and the create-react-class method.
For a detailed article for ES6, please refer: Introduction to ES6
Creating React Application And Installing Module:
- Step 1: Create a React application using the following command.
npx create-react-app foldername
- Step 2: After creating your project folder i.e. foldername, move to it using the following command.
cd foldername
- Step 3: After creating the ReactJS application, Install the create-react-class modules using the following command.
npm install create-react-class
Project Structure:
1. Use of Create-react-class: It is used as an alternative to default class-based component.
ES6 implementation:
Filename: App.js javascript import React from 'react' import "./App.css" // Make a App class component using ES6 class App extends React.Component { render() { return <h1>Hello Welcome to, GeeksforGeeks</h1>; } } export default App;
Create-react-class module:
Filename: App.js javascript import React from 'react' import "./App.css" // Created a variable createReactClass which // holds a main create-react-class module // with require module var createReactClass = require('create-react-class'); // App takes a variable and renders // the output function for showing // results in screen var App = createReactClass({ render: function () { return <h1>Hello Welcome to, GeeksforGeeks</h1>; } }); export default App;
Output:
2. Declaring Default props in React: With ES6, default props can be used as a property of reactjs class component.
ES6 implementation:
Filename: App.js javascript import React from 'react' import "./App.css" class App extends React.Component { render() { // we set the default value // of props and called with the help of es6 return <h1>Hello Welcome to, {this.props.name}</h1>; } } // Assigned the default value of props. App.defaultProps = { name: 'GeeksforGeeks' } export default App;
Create-react-class module:
JavaScript import React from 'react' import "./App.css" var createReactClass = require('create-react-class'); var App = createReactClass({ // We added this getDefaultProps // function parameter as a passed object // for getting props values. // We return all the name with return function getDefaultProps: function () { return { name: "GeeksforGeeks" } }, render: function () { // We call the name from here! return <h1>Hello Welcome to, {this.props.name}</h1>; } }); export default App;
Output:
3. Setting Initial state: Same as declaring default props, setting initial state is the same. For setting initial state, we need to assign this.state in the constructor. For this Make Counter.js file.
ES6 implementation:
Filename: Counter.js javascript import React from 'react' class Counter extends React.Component { constructor(props) { super(props); // setting the initial count this.state = { count: props.initialCount }; } handleClick = () => { // for increasing by 1 with on click this.setState({ count: this.state.count + 1 }); } render() { return ( <button onClick={this.handleClick}> {this.state.count} </button> ); } } export default Counter;
Create-react-class module Implementation:
Filename: Counter.js javascript import React from 'react' var createReactClass = require('create-react-class') var Counter = createReactClass({ // Use of getInitialState method for initial state getInitialState: function () { // Setting the initialCount return { count: this.props.initialCount }; }, handleClick: function () { // Incrementing by 1 this.setState({ count: this.state.count + 1 }); }, render: function () { // Output return <button onClick={this.handleClick}> {this.state.count} </button> } }); export default Counter
Render File: In this file we will call the Counter.js file.
Filename: App.js javascript import React from "react"; import "./App.css" import Counter from './Counter'; // Make a App class component using ES6 class App extends React.Component { render() { // Passing the value of initialCount; return <h1><Counter initialCount={0} /></h1>; } } export default App;
Output:
4. Auto binding in ES6 - Es6 class component has the same semantics as the regular es6 class. It does not bind 'this' as default we need to explicitly bind 'this' for getting it to work. But create-react-class module automatically binds.
ES6 implementation:
Filename: Counter.js javascript import React from 'react' class Counter extends React.Component { constructor(props) { super(props); this.state = { count: props.initialCount }; this.handleClick = this.handleClick.bind(this) } handleClick() { this.setState({ count: this.state.count + 1 }); } render() { return ( <button onClick={this.handleClick}> {this.state.count} </button> ); } } export default Counter;
Create-react-class module implementation: Same as setting the initial state example.
Render File: In this file we will call the Counter.js file.
Filename: App.js javascript import React from "react"; import "./App.css" import Counter from './Counter'; // Make a App class component using ES6 class App extends React.Component { render() { // passing the value of initialCount; return <h1><Counter initialCount={0} /></h1>; } } export default App;
Output:
Note: Please make sure that ES6 implementation of setting initial state is an alternative to bind. this, i.e using the arrow function in the handleClick function resolves the issue for ES6.
For auto binding:
- Use bind. this in the handleClick function.
- Use arrow function for handleClick function
- Use create-react-class.
Reference: https://reactjs.org/docs/react-without-es6.html
Similar Reads
React.js without JSX
JSX: Consider the following code snippet, const sample = <h2>Greetings</h2>; The above code snippet somewhat looks like HTML, and it also uses a JavaScript-like variable but is neither HTML nor JavaScript, it is JSX. The JSX is basically a syntax extension of regular JavaScript and is us
2 min read
Why does React use JSX?
React uses JSX (JavaScript XML) as a syntax extension for JavaScript. JSX is a syntax extension that looks similar to XML or HTML but is designed to work seamlessly with JavaScript. Reasons why React uses JSX:Readability and Expressiveness: JSX provides a more concise and readable syntax for definin
2 min read
What is React?
React JS is a free library for making websites look and feel cool. It's like a special helper for JavaScript. People from Facebook and other communities work together to keep it awesome and up-to-date. React is Developed by Facebook, React is a powerful JavaScript library used for building user inte
6 min read
What is âReact Fiberâ?
In this article, we will learn about React Fiber. React Fiber is a concept of ReactJS that is used to render a system faster and smoother. React is one of the popular JavaScript library used to create a responsive user interface. React makes coding simple as compared to other frameworks. After certa
7 min read
React JS ReactDOM
ReactDom is a core react package that provides methods to interact with the Document Object Model or DOM. This package allows developers to access and modify the DOM. Let's see in brief what is the need to have the package. Table of Content What is ReactDOM ?How to use ReactDOM ?Why ReactDOM is used
3 min read
ReactJS ES6
ES6, also known as ECMA script 2015 is a scripting language which is based on specification and standardization by ECMA International Company in ECMA-262 and ISO/IEC 1623. It is the sixth edition of the ECMAScript language specification standard. It was created to standardize JavaScript language to
6 min read
7 Best React.js Frameworks to Use
A website or any application contains two parts, frontend and backend. It is necessary to have strong backend services and frameworks to use in order to maintain the siteâs availability and to manage the traffic load that might occur during certain scenarios. Creating interactive and good user inter
9 min read
NextJS vs React
NextJS is a framework of React that enhances its server-side rendering, automatic code splitting, and simplified routing, while React is a frontend library of JavaScript that is used for developing interactive user interfaces and building UI components. NextJS is optimized for production with easier
13 min read
Top 7 Best Books to Learn React JS
You might have heard the saying, Hard-work is the key to success, which might have been relative in the past, but now the scenario has changed. Now the world has developed so much that only doing hard wonât guarantee success; you will have to do smart work. ReactJs is the most popular front-end libr
6 min read
What are the advantages of React.js ?
React.js is a popular JavaScript library used for building dynamic and interactive user interfaces. It has become a go-to tool for developers due to its efficient architecture, high performance, and ease of use. This article highlights the key advantages of using React.js for web development and exp
5 min read