This is the way to define the react class components without ES6. We take the help of the create-react-class module.
Firstly, We require the create-react-class module in our file. Then we create a variable app that uses the createReactClass API to define the render function.
import React from "react";
import "./App.css";
var createReactClass = require('create-react-class');
var App = createReactClass({
render: function() {
return <h1>Hello World</h1>;
}
});
export default App;
You can also try this code with Online Javascript Compiler
To demonstrate the concept of autobinding, we will use a similar example. We will use a button that shows the number of times it has been clicked.
With ES6
In React components declared as ES6 classes, methods follow the same semantics as regular ES6 classes. This means that they don’t automatically bind this to the instance. You’ll have to use .bind(this) in the constructor explicitly.
Counter.js
In the Counter.js, inside the constructor, we explicitly use .bind(this) keyword to bind the handleClick method to use it inside the render method.
This implies that ES6 classes come with a little more boilerplate code for the event handlers. But the upside is that it has slightly better performance in large applications.
Few Options for Autobinding
There are a few options that will help to achieve autobinding. They are:
Use createReactClass
Use arrow functions to define event handlers
Bind methods in the constructor.
Frequently Asked Questions (FAQs)
What is ES6?
ES6 is the 6th version of the ECMAScript programming language. ECMAScript is the standardized javascript language released in 2015.
Which node module can be used to use React without ES6?
We can install the create-react-class module when using react without ES6.
How do we implement autobinding with ES6?
In React with ES6, The binding of this keyword is to be done explicitly using .bind(this) in the constructor.
Key Takeaways
We learned about the concept of using react without ES6. This is an exciting concept that gives us a more profound understanding of React. This article also explained the differences in the use of React with ES6 and React without ES6. We also learned about the implementation of autobinding in React without ES6.
Apart from this, you can also expand your knowledge by referring to these articles on Javascript and React.
You can also consider our React Js Course to give your career an edge over others.