What Is ReactJS
What Is ReactJS
Features
Benefits
➢ Easy to learn
➢ Easy to adept in modern as well as legacy application
➢ Faster way to code a functionality
➢ Availability of large number of ready-made component
➢ Large and active community
Disadvantages of 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.
Applications
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
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
ReactDOM.render(
document.getElementById('app')
);
</script>
</body>
</html>
Make sure you have nodejs installed. If not installed install node.js in our system.
D:\>md reactproj
D:\>cd reactproj
D:\reactproj>
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
ReactDOM.render(
document.getElementById('root')
);
Index.html
<!DOCTYPE html>
<html>
<head>
<title>ReactJS Demo</title>
</head>
<body>
<div id = "root"></div>
</body>
</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:
"scripts": {
Like this:-
"dependencies": {
"react": "^18.2.0"
},
"scripts": {
---------------------------------→
To start testing reactjs run the command
Sometime ask confirmation to start server by port 3000 Enter your choice ‘y’
Add all your js and .jsx file in src/ folder .The file structure will be as follows:
reactproj/
src/
index.js
node_modules/
public/
index.html
package.json
1. To start with ReactJS, we need to first import the react packages as follows.
We will write a simple code in this tutorial React JS, wherein we will display the message “Hello, Elysium Academy!”
ReactDOM.render(
document.getElementById('root')
);
ReactDOM.render will add the <h1> tag to the element with id root. Here is the html file we are having:
<!DOCTYPE html>
<html>
<head>
<title>ReactJS Demo</title>
</head>
<body>
<div id = "root"></div>
</body>
</html>
Next in this React.js Tutorial, we need to compile the code to get the output in the browser.
reactproj/
node_modules/
src/
index.js
package.json
public/
index.html
We have added the commands to compile the final file in package.json as follows:
"scripts": {
},
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
The URL http://localhost:3000 will open in the browser once the code is compiled as shown below:
ReactJS – Installation
React provides CLI tools for the developer to fast forward the creation, development and deployment of the React based web application.
React CLI tools depends on the Node.js and must be installed in your system.
Hopefully, you have installed Node.js on your machine. We can check it using the below command –
>node –version
If Nodejs is not installed, you can download and install by visiting https://nodejs.org/en/download/.
Toolchain
To develop lightweight features such as form validation, model dialog, etc., React library can be directly included into the web application
through content delivery network (CDN). It is similar to using jQuery library in a web application.
For moderate to big application, it is advised to write the application as multiple files and then use bundler such as webpack, parcel, rollup,
etc., to compile and bundle the application before deploying the code.
React toolchain helps to create, build, run and deploy the React application. React toolchain basically provides a starter project template
with all necessary code to bootstrap the application.
The serve is a lightweight web server. It serves static site and single page application. It loads fast and consume minimum memory. It can
be used to serve a React application.
Let us install the tool using npm package manager in our system
After successfully install to run serve web server your work place like this.
Next, open the browser and enter http://localhost:3000 in the address bar and press enter. serve application will serve our webpage as
shown below.
Hello
The serve will serve the application using default port, 5000. If it is not available, it will pick up a random port and specify it.
Babel compiler
Babel is a JavaScript compiler which compiles many variant (es2015, es6, etc.,) of JavaScript into standard JavaScript code supported by all
browsers. React uses JSX, an extension of JavaScript to design the user interface code. Babel is used to compile the JSX code into JavaScript
code.
To install Babel and it’s React companion, run the below command −
...
...
+ babel-cli@6.26.0
+ babel-preset-react-app@3.1.2
Babel helps us to write our application in next generation of advanced JavaScript syntax.