ReactJS is maintained by Facebook and a community of individual developers and companies. It is widely used as a base in building single-page websites and mobile applications.
Run the React Application
Adding Time Travel
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.
Kickstart your career
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.
Create React App
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.
Working with External CSS in ReactJS
Setup for the Tutorial
Replace version with the latest react version for both react-development.js and react-dom.developement.js.
What are Components in ReactJS?
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.
Here is the working ReactJS example using cdn files and babeljs script.
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.
Before We Start the Tutorial
In production, you will have to install react using npm package. 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.
What You Should Already Know
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/.
What is JSX?
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.
Completing the Game
Create a file index.js and add import react and react-dom, as shown below. We have returned the basic code for reactjs. We will explain the details of it in the next chapter.
Working with Events in ReactJS
We want to display Hello, from Guru99 Tutorialsand 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.
Learning by Exercises
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:.
Working with Forms
After installing all the packages and adding the above command, the final package.json is as follows:. To start testing reactjs run the command. It will open browser with url http://localhost:3000/ as shown below:.
Features of ReactJS
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. 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!”.
What are Props in ReactJS?
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.
Disadvantages of ReactJS
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.