In this tutorial, we will learn how to install React JS in Windows.
React is a library that helps us to build and create front-end interfaces for Single page applications and it utilizes the power of virtual DOM. usereducer in react will make you understand better about React hooks.
React was an in-house project of Facebook, and it was made open to the public in the year 2013. The adoption of ReactJS has seen an upward curve since its advantages have been realized. Various startups and established organizations are now adopting the technology and upgrading their technology stack. For information, check out Full Stack Developer courses.
1. Prerequisite for Windows
To install React on Windows, your system will require the minimum configuration as below:
- Windows XP, Windows 7 (32/64 bit) or higher
- Minimum 4 GB RAM and higher
- 10 GB available space on the hard disk
- At least one Internet Browser e.g. Chrome, Firefox, Microsoft Edge etc.
- Node.js
- Active internet connection minimum speed 512kbps and above.
- At least one installed code Editor to test and debug your code e.g.
2. Introduction to React.js
ReactJS is a library written in TypeScript. It utilises the syntax of the modern version of JavaScript as described by ES6 and its higher version.
Applications built using ReactJS use the Single reusability principle. This advocates the idea of building web pages and applications using components and unidirectional flow. In React we have the concept of states and the concept of immutability. Components have hierarchy in terms of Parent and Child components. A component in case of React can be thought of as a piece of code which is based on the principle of pure functions. We will look into the pure component later. First, let’s understand what a state is. For e.g. To become a member of a service, the user fills his information on the registration page. While filling the details there can be many states of the form, for e.g. When the form field is empty or when the form field has witnessed some error on some particular field, which needs to be corrected; or when after correction, the form data has been validated and is ready for submission. So, at a broad level, the registration form has been through various states. These states represent at which level the application is, in terms of interacting with the end-user. Each level of interaction for this form is represented by the state, from an empty form to being a fully filled form with a display of an error for certain fields and the validated form. In React, we have the component based on the pure function. A pure function can be memorised as a piece of code, which does one task and does it pretty well. For a certain input, it always returns the same output, so this means we are increasing predictability of the code. Since React.js follows a certain code pattern and principles in order to work, it lowers the curve of the knowledge gap; whether you are one-person or a team of developers working mutually.
3. Introduction to Node.js and NPM on Windows 10
To run ReactJS we will require Node.js on our system.Node.js is a server which will help us to run the React code. It is based on non-blocking input and output and the Chrome V8 JavaScript engine. The Node.js code is open source.
NPM which is an abbreviation of Node package manager, npmjs.com is supported by various developers around the world. It has various node modules, using which developers can host and publish their modules on the open-source community. It hosts modules in private and public visibility. A module carries code which exists to serve high or low level functionalities. In terms of code adoption and availability of various modules it gives an edge and tries to make the developer more productive.
We can plug in and plug out the module. Some modules are dependent on other modules; which is defined as dependency among modules.
While building an application, a developer can pick the module, tweak and remix it to suit the application needs, and can then release to the open-source community. So, instead of reinventing the wheel, it is like picking a wheel (npm module) from npmjs.com, giving it further momentum and giving it back to the open source community.
4. Download and Install Node.js
To install Node.js we need to go to the URL
Depending upon our Windows OS version in terms of 32 Bit or 64 Bit, we can pick the installer and install that version.
In this tutorial I am using Windows 8, 64 Bit.
The Node.js installer includes NPM. It is best to install the even numbered version of NPM.
Depending upon your operating system, the Nodejs.org home page will show the Download button and recommended LTS version.
After the download is complete we will go to the downloads folder and run the installer.
The installer will show the below Setup Wizard. Click next.
The next screen will ask for the End-user License Agreement. Select the checkbox at the bottom right to provide your consent and click on Next to proceed with the installation.
The installer will ask for Destination folder and the default path set by installation is C:\Program Files\nodejs\
Click on Next button
The above screen is an important step in the installation process. And if you see closely it also sets the environmental path variables to command prompt on Windows. To begin your journey in web development, learn Web Development.
Click on Next to continue with the installation.
The Windows OS may ask you to allow Node.js installation and make changes.Click on Yes button.
During the installation, if you have allowed for Chocolatey and required modules installation for C++ and Python, you will see the UI below in the command prompt. This installation requires 3 Gb of free disk space. In this tutorial this step is not required, so we are skipping this step by closing the window.
If you are interested in installing it, press Enter to continue.
Once the installation is complete you need to verify the Node.js installation.
For this, we will use the command prompt.
To run command prompt
Press keys Win+R
And type cmd in the window below.
Next Click on Ok or Press Enter on the keyboard.
5. Installation of React
After installation of Node.js, we need to install React. To check the Node.js version, open the Windows command prompt.
Press Win+R and type cmd.
In the command line, type
node -v to see its version.
We can also check for npm version, which is installed with Node.js, with the following command
npm -v
After running these commands, we can check the node version v14.15.1 and npm version 6.14.8
As we have confirmed the Node.js installation we can proceed to the next steps.
While in the command prompt, we have navigated to a folder called Codefactory by following the command cd Codefactory
In this folder, we have created a folder called react-windows by using the command mkdir react-windows.
After the folder react-windows has been created, we will change the directory to react-windows with the command
cd react-windows
ReactJS can be installed in various ways.
Now, we will type npm init. It will ask for the below configuration line by line.
Insert your input, followed by Enter keypress to proceed with the next configuration.
At the end of the configuration it will confirm for the inputs you have entered. If you are happy with the configuration data, type yes and enter to continue.
The npm init will help us to create a package.json file.
Now, the next step to install React requires us to go to the command prompt and type the following command in the react-windows directory.
npm install --save react
And after the above command npm install --save react-dom
Behind the scene, these commands fetch the specified module from npmjs.com and download it in the local codebase.
Let's have a look at the react-windows folder. Here we can see some newly created directories in node_modules.
So, in this tutorial, we have learned to install React and reactDOM.
But to see the ReactJS SPA (single page app) there is more work to be done in the above code.
As an alternative and fast approach we can do it via create-react-app
Let us move to Codefactory folder and with the command cd.. create another folder react-cli
Next, type the following command mkdir react-cli
Now we will use create-react-app module and type the following command
Please note that my-fast-app is the name of your app. This is an example and you can be creative in choosing your own name.
npx create-react-app my-fast-app
If we see closely it will take care of the rest of the installation steps, and react, react-dom and other related modules are installed automatically.
This process is a little data intensive, so please be patient while the download and installation happens
When the above step gets completed the command prompt displays the below output.
Now, let us run our first react app, by navigating to my-fast-app directory as below
cd my-fast-app
And enter the next command as npm start
The npm command will show the application in the browser. http://localhost:3000
And if you are running node.js for the first time using npm command, it will ask for permission to allow access and we need to allow access to run.
As we are using a code editor we can have a look at the directory structure and some of the important files, such as index.html in the public folder, in src folder the index.js and App.js. The src folder contains the react component which we can build further on this codebase.
index.js is the js invocation point for react app.This index.js is linked with the App.js, which is responsible for showing the content in the browser. That’s what we see on the demo page.
Let’s edit the App.js by going to line 10 and adding the following code
Talk is cheap, show me the <code>Code</code>
Once you save the file by Ctrl+S
The code will be auto refreshed in the browser, after compiling.
It will show us the following output.
So, now feel free to change messages and alter the page layout and structure for experimentation.
If you are familiar with CSS, you may also change the page style using App.css and tinker with the code as well.
Summary
In this tutorial, we have introduced you to React JS. Its impact in terms of building modern front end interfaces using component-based architecture is significant. We have also touched upon the concept of states, immutability and pure functions.
We have got a brief introduction to the Node.js server and NPM modules, the capabilities of Node.js server and the power of the open source community in the npmjs.com.
To install React in Windows, Node.js installation is a prerequisite.
There are various methods for installation. Once we have installed Node.js, React can be installed either by npm commands or by using the create-react-app module.