What is ReactJS?
Features of ReactJS
Advantages of ReactJS
Disadvantages of ReactJS
Using ReactJS from CDN
Using NPM Packages
What is JSX?
What are Components in ReactJS?
What is a State in ReactJS?
What are Props in ReactJS?
Life Cycle of a Component
Working Example
Working with Forms
Working with Events in ReactJS
Working with Inline CSS in ReactJS
Working with External CSS in ReactJS
Features of ReactJS
JSX : JSX is an extension to javascript. Though it is not mandatory to use JSX in react, it is one of the good features and easy to use. Components: Components are like pure javascript functions that help make the code easy by splitting the logic into reusable independent code. We can use components as functions and components as classes. Components also have a state, props which makes life easy. Inside a class, the state of each of the props is maintained. Virtual DOM: React creates a virtual dom, i.e., in-memory data -structure cache. Only the final changes of DOM has later updated in the browsers DOM. Javascript Expressions: JS expressions can be used in the jsx files using curly brackets, for example {}.
Advantages of ReactJS
Here, are important pros/benefits of using ReactJS:
ReactJS uses virtual dom that makes use of in-memory data-structure cache, and only the final changes are updated in browsers dom. This makes the app faster. You can create components of your choice by using the react component feature. The components can be reused and also helpful in code maintenance. Reactjs is an open-source javascript library, so it is easy to start with. ReactJS has become very popular in a short span and maintained by Facebook and Instagram. It is used by many famous companies like Apple, Netflix, etc. Facebook maintains ReactJS, the library, so it is well maintained and kept updated. ReactJS can be used to develop rich UI for both desktop and mobile apps. Easy to debug and test as most of the coding is done in Javascript rather than on Html.
Disadvantages of ReactJS
Here, are cons/ drawbacks of using ReactJS:
Most of the code is written in JSX, i.e., Html and css are part of javascript, it can be quite confusing as most other frameworks prefer keeping Html separate from the javascript code. The file size of ReactJS is large.
Using ReactJS from CDN
To start working with react, we need to first install reactjs. You can easily get started to use reactjs by using the CDN javascript files, as shown below. Go to the official site of reactjs to get the CDN links, i.e., https://reactjs.org/docs/cdn-links.html and you will get the required files to explain the following image.
For dev
For prod:
Replace version with the latest react version for both react-development.js and react-dom.developement.js. You can host the files at your end to start working with reactjs. In case if you are planning to use the CDN files, make sure to keep the cross-origin attribute, to avoid cross-domain issues. Reactjs code cannot be executed directly in the browser and needs to be transpired using Babel to javascript before executing in the browser. Here is the BabelJS script that can be used: Here is the working ReactJS example using cdn files and babeljs script. Output:
We will get into the details of the code in the next chapter, let us see the working here with CDN files. It is said that using babel script directly is not a good practice, and newcomers can just use it to learn reactjs for now. In production, you will have to install react using npm package.
Using NPM Packages
Make sure you have nodejs installed. If not installed, go through this tutorial for nodejs (https://www.guru99.com/node-js-tutorial.html) installation. Once you have nodejs installed, create a folder reactproj/. To start with project setup, run command npm init. This is how the folder structure will look like: Now we will install the packages that we need. Here are the list of packages for reactjs: Open the command prompt and run above commands inside the folder reactproj/. Create a folder src/ where all the js code will come in that folder. All the code for reactjs project will be available in the src/ folder. Create a file index.js and add import react and react-dom, as shown below. index.js We have returned the basic code for reactjs. We will explain the details of it in the next chapter. We want to display Hello, from Guru99 Tutorials and the same is given to the dom element with id “root”.It is taken from the index.html file, which is the start file, as shown below. Create a folder public/ and add index.html in that as shown below index.html The package react-scripts will take care of compiling the code and starting the server to display the html file i.e index.html. You need to add the command in package.json that will take care of using react-scripts to compile the code and start server as shown below: After installing all the packages and adding the above command, the final package.json is as follows: Package.json To start testing reactjs run the command It will open browser with url http://localhost:3000/ as shown below: public/index.html
We are going to use the same process to execute the javascript files in the next chapters too. Add all your js and .jsx file in src/ folder .The file structure will be as follows:
How to Create Your First React Project Setup
Here is a step by step guide in this ReactJS Tutorial to start with the first react application. Step 1) Import the react packages. 1. To start with ReactJS, we need to first import the react packages as follows. 2. Save the file as index.js in src/ folder Step 2) Write Simple Code. We will write a simple code in this tutorial React JS, wherein we will display the message “Hello, from Guru99 Tutorials!” ReactDOM.render will add the
tag to the element with id root. Here is the html file we are having:
Step 3) Compile the Code.
Next in this React.js Tutorial, we need to compile the code to get the output in the browser.
Here is the folder structure:
We have added the commands to compile the final file in package.json as follows:
To compile the final file run following command:
When you run above command, it will compile the files and notify you if any error, if all looks good, it will open the browser and the run the index.html file at http://localhost:3000/index.html
Command: npm run start:
Step 4) Check Output. The URL http://localhost:3000 will open in the browser once the code is compiled as shown below:What is JSX?
JSX is an extension to javascript. It is a template script where you will have the power of using HTML and Javascript together. Here is a simple example of a JSX code.
Why we need JSX in React?
For a UI, we need Html, and each element in the dom will have events to be handled, state changes, etc. In case of React, it allows us to make use of Html and javascript in the same file and take care of the state changes in the dom in an efficient manner.
Expressions in JSX
Here is a simple example of how to use expressions in JSX. In earlier ReactJS examples, we had written something like : index.js We will now change the above code to add expressions. Expressions are used inside curly brackets {}, and they are expanded during run time. Expressions in react are the same as javascript expressions. index.js Let us now test the same in the browser.
You can see that the {display} expression is not replaced. React does not know what to do when an expression is used inside the .js file. Let us now add changes and create a .jsx file, as shown below: test.jsx We have added the required code and will use the text.jsx file in index.js.We want the h1tag variable to be used inside script.js, so the same is exported as shown above in the test.jsx Here is the modified code in index.js
We can use the h1tag now in the ReactDOM.render as shown below: Here is the output when we check the same in the browser:
What are Components in ReactJS?
Components are like pure javascript functions that help make the code easy by splitting the logic into reusable independent code.
Components as functions
test.jsx
The Component Hello is used as an Html tag, i.e.,
Class as Component
Here is a ReactJS example that uses a class as a component.
test.jsx
We can use Hello component in index.js file as follows:
index.js
The Component Hello is used as an Html tag i.e.,
What is a State in ReactJS?
A state is a javascript object similar to props that have data to be used with the reactjs render. The state data is a private object and is used within components inside a class.
Example of State
Here is a working example on how to use state inside a class. test.jsx index.js This is what we get when we test it in the browser:
What are Props in ReactJS?
Props are properties to be used inside a component. They act as global object or variables which can be used inside the Component.
Props to Function Component
Here is an example of passing props to a function component.
As shown above, we have added msg attribute to
Props to Class Component
To access the props in a class we can do it as follows: test.jsx The msg attribute is passed to the component in index.js as follows: This is the output in the browser:
Also Check:- AngularJS Tutorial for Beginners: Learn AngularJS Step by Step
Life Cycle of a Component
A component life cycle is divided into Initialization, Mounting, Update, and UnMounting stages. Here is a detail explanation about each Component. A component in reactjs has the following stages : Initialization: This is the first stage of the component life cycle. Here it will have the default props and the state at the initial level. Mounting: In this phase, the Component is rendered inside the dom. We having exposure to following methods in the mounting state.
componentDidMount(): This is also called when the Component is just added to the dom.
render(): You have this method for all the components created. It returns the Html node.
Update: In this state, the dom is interacted by a user and updated. For example, you enter something in the textbox, so the state properties are updated. Following are the methods available in update state:
shouldComponentUpdate() : called when the component is updated.
componentDidUpdate() : after the component is updated.
UnMounting: this state comes into the picture when the Component is not required or removed. Following are the methods available in unmount state: Component willUnmount(): called when the Component is removed or destroyed.
Working Example
Here is a working example which shows the methods called at each state. Example: complife.jsx index.js When you check the output in the browser
In browser console you get :
When the user enters in the textbox:
In console following messages are displayed:
Working with Forms
In reactjs Html input elements like ,
Step 1) Enter your name in the textbox:
Click on submit button
Working with Events in ReactJS
Working with events in reactjs is same as how you would have done in javascript. You can use all the event handlers that are used in javascript. The setState() method is used to update the state when the user interacts with any Html element. Here is a working example of how to use events in reactjs. events.jsx For the input fields, we need to maintain the state, so for that react provides a special method called setState, which helps to maintain the state whenever there is a change. We have used events onChange and onClick on the textbox and button. When the user enters inside the textbox the onChange event is called, and the name field inside state object state is updated as shown below: index.js Here is the output in the browser:
When a user enters the name :
When the user clicks on the Click Here button:
Working with Inline CSS in ReactJS
Will take a look at a working example to understand the working of inline css in reactjs. addstyle.jsx I have added color: ‘red’ style to the h1 tag. index.js The output in the browser is as follows:
You can create an object of style that you want on the element and use an expression to add style, as shown in the example above.
Working with External CSS in ReactJS
Let us create a external css , for that create a folder css/ and add style.css in it. style.css Add the style.css to your index.html file Now let us add the class to the h1 tag in .jsx file addstyle.jsx index.js The className attribute is given the class details. Now let us test in the browser.
This is what you see when you inspect the h1 tag in browser:
You can see that the class=” h1tag” is added successfully to the h1 tag.
Summary:
ReactJS is an open-source front-end JavaScript library to build the user interface. It is maintained by Facebook and used by many companies today for UI development. The core features of ReactJS includes JSX, components(functional components and class-based components), the life cycle of a component, props, and state support for a component, working with javascript expressions. Project setup of ReactJS is explained using CDN files and also using npm packages to build the project. JSX is an extension to javascript. It is a template script where you will have the power of using Html and javascript together. Components are like pure javascript functions that help make the code easy by splitting the logic into reusable independent code. A state is a javascript object similar to props that have data to be used with the reactjs render. The state data is a private object and is used within components inside a class. Props are properties to be used inside a component. A component life cycle is divided into Initialization, Mounting, Update, and UnMounting stages. In reactjs html input elements like , and has their own state and needs to be updated when user intereacts using the setState() method. Working with events in reactjs is same as how you would have done in javascript. You can use all the event handlers that are used in javascript. The setState() method is used to update the state when the user interacts with any Html element. ReactJS allows you to work with external css as well as inline css using javascript expression.
Also Check:- Top 70 React Interview Questions and Answers (Updated)