Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
What is “React Fiber”?
Next article icon

React.js without ES6

Last Updated : 11 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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


 


Next Article
What is “React Fiber”?
author
shiv_ka_ansh
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • ES6

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences