In this article, I am going to create a simple React application with and without Babel. Often, for creating React applications, we need to install some packages and tools, like NPM, Webpack, and Babel. Here, the question is, why do we require all these packages? Can’t we create a React application without them? What is the role of these packages in React applications?
So, while going through this article, we will find the answers to all of the above questions. First of all, I am introducing the packages and tools required by a React application.
npm
npm stands for Node Package Manager, which is a command line interface program to manage the node.js libraries. We use npm in React applications because they allow us to install JavaScript libraries.
Webpack
Webpack is a static module bundler for modern JavaScript applications. When Webpack processes our application, it internally builds a dependency graph which maps every module our project needs and generates one or more bundles. When we create a React application, there is a chance of creating more than one module in the application. So, Webpack is used to bundle all these modules with their dependencies.
Babel
Babel is a tool that is mainly used to convert the ECMA Script 2015+ code into a backward compatible version of JavaScript in current and older browser or environment. So, Babel converts our React application's code (if it is written in ECMAScript) into JavaScript so that it can be rendered by all the browsers.
I hope now it is understood what kind of packages are required for React applications and what is the role of these packages in a React application.
Interestingly, these are not the must-have requirements for creating a React application. We can create a React application without using these packages. It’s true. For creating React applications, the least requirements are React and the ReactDOM package.
- React package helps us in creating a component.
- ReactDOM package helps us to render the component.
Let’s understand with a simple project.
Create an HTML file called “ReactWithoutBabel.html” which simply contains the following code.
- <html>
- <head>
- <title>React App Without Babel</title>
- </head>
- <body>
- </body>
- </html>
Now, for creating the React app, the basic requirement is to have a React package. For this, we use 2 CDN links, one for React and the second for ReactDOM. Below are the CDN link.
- <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.development.js"></script>
- <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.development.js"></script>
Use these two CDN links into HTML page. Copy and paste it below the title tag. After this, create a div inside the <body> tag with “id” attribute and give a specific id to it, like I have given below.
Now, we will be creating a React component. Since React is a JavaScript library, we write the React code inside the <script> tag.
- <script>
- class YourComponentName extends React.Component {
- render() {
-
- }
- }
- </script>
In the above code, change “YourComponentName” as per our component name. For example, we can create a component having the name “First”.
So, our component looks like below.
- <script>
- class First extends React.Component {
- render() {
-
- }
- }
- </script>
As we see, for creating a component, we require the Component class, and to use this class, we need to extend it. Inside the component class, there is a render() method which is responsible for creating React elements.
- <script>
- class First extends React.Component {
- render() {
- return React.createElement('div', null, 'C# Corner');
- }
- }
- </script>
In the above code, we used createElement() for creating an element. It takes three arguments -
- type - type of the HTML element or component like div, h1,h2,p, button etc.
- props - it is used for properties of object like style, class name or event handler etc.
- children - anything which we need to pass between the DOM elements.
A component with elements is created. Now, let us render it into DOM. For this, we use ReactDOM.render().
- <script>
- class First extends React.Component {
- render() {
- return React.createElement('div', null, 'C# Corner');
- }
- }
- ReactDOM.render(
- React.createElement(Test, null), document.getElementById("test")
- )
- </script>
It takes two arguments -
- The first argument tells us which component or element needs to be rendered in the DOM.
- The second argument tells us where to render in the DOM.
In the above code, we didn’t use Babel. So, we created another element where our component is rendered.
Now, our HTML page looks like below.
- <html>
-
- <head>
- <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.development.js"></script>
- <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.development.js"></script>
- <title>React App Without Babel</title>
- </head>
-
- <body>
- <div id="test"> </div>
- <script>
- class First extends React.Component {
- render() {
- return React.createElement('div', null, 'C# Corner');
- }
- }
- ReactDOM.render(
- React.createElement(First, null), document.getElementById("test")
- )
- </script>
-
- </body>
-
- </html>
And if we run this program, the output will be something like below.
I hope you understood how a React application without Babel can be created; however, with the help of Babel, the code is very simple to write. Let's have a look.
- <html>
-
- <head>
- <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.development.js"></script>
- <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.development.js"></script>
- <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
- <title>React App With Babel</title>
- </head>
-
- <body>
- <div id="test"> </div>
- <script type="text/babel">
- class First extends React.Component {
- render() {
- return(
- <div>
- React Application with Babel
- </div>
- );
- }
- }
- ReactDOM.render(<First/>, document.getElementById("test"));
- </script>
-
- </body>
-
- </html>
First, import the CDN for Babel and add the type attribute inside the script, i.e., type =”text/babel”. After that, we can see that there are a lot of changes in the “First” component as well as in the ReactDOM.render() method. Now, we simply used JSX format in the component and Babel will convert it into simple JavaScript.
And the output of the above code is shown below.
Conclusion
Now, we have learned how can we create a React component with and without Babel and other packages. For practice purposes, I have attached "ReactSample" which contains two HTML files - “ReactWithBabel” and “ReactWithoutBabel” - with this article. The files cover all the pieces of the code mentioned in this article. You can download and modify this as per your different requirements.
All the queries related to this article and sample files are always welcome. Thanks for reading!!!