What Is React: Understanding the Features and How to Deploy for Modern Web Development
React is one of the most popular JavaScript libraries for developing mobile and web applications. Developed by Meta (formerly Facebook), React empowers developers to create reusable components to build user interfaces (UIs).
It’s important to note that React is not a framework. That’s because it’s only responsible for rendering the components of an application’s view layer. However, React offers an alternative to frameworks like Angular and Vue, enabling you to construct complex functionalities alongside it.
This article will explain how React works, explore its features, and discuss the benefits of using React.js for front-end developers. Furthermore, we’ll compare React.js vs React Native, discussing their web and mobile app development functions.
Additionally, we will guide you through deploying a React application on Hostinger’s VPS.
Download Glossary For Web Beginners
Learn What Is React With Hostinger Academy
What is React.js? Watch this video to learn more about React.js.
What Is React?
React is a powerful JavaScript library to create interactive user interfaces with building blocks. It revolutionized UI development by introducing a declarative and component-based approach. Unlike manual Document Object Model (DOM) manipulation, as seen in jQuery, React employs a Virtual DOM to optimize performance.
React JavaScript, or React.js, handles complex UIs, offering developers a structured and efficient way to create interactive, data-driven interfaces.
How Does React Work?
React introduces JSX, a syntax extension that seamlessly combines JavaScript and HTML code. JSX tags resemble XML with a few distinctions.
For instance, React uses className instead of the traditional class attribute for CSS classes. Numeric values and expressions are enclosed in curly brackets, while quotation marks denote strings, similar to JavaScript.
The following is an example of a React code snippet:
<MyCounter count={3 + 5} />; const GameScores = { player1: 2, player2: 5 }; <DashboardUnit data-index="2"> <h1>Scores</h1> <Scoreboard className="results" scores={GameScores} /> </DashboardUnit>;
Here’s a breakdown of the React code snippet above:
- <MyCounter count={3 + 5} />; – this line renders a MyCounter React component with the count prop set to 8.
- const GameScores = { player1: 2, player2: 5 }; – this code creates an object, GameScores, with player scores.
- <DashboardUnit data-index= “2”> – it renders a DashboardUnit component with a data-index attribute set to 2.
- <h1>Scores</h1> – this line renders an <h1> heading element with the text Scores.
- <Scoreboard className= “results” scores={GameScores} /> – this renders a Scoreboard component with a CSS class and game scores passed as attributes.
React also leverages the Virtual DOM to optimize performance by minimizing direct DOM manipulation. It calculates the differences between the current and previous Virtual DOMs when changes occur, efficiently updating the real DOM.
const element = <h1>Hello, world!</h1>; ReactDOM.render(element, document.getElementById('root'));
The code snippet above can be explained as follows:
- const element = <h1>Hello, world!</h1>; – this line creates a variable named element and assigns it a JSX element. In this case, an <h1> element containing the text Hello, world!.
- ReactDOM.render(element, document.getElementById(‘root’)); – this code uses the render method to update a JSX element into the real DOM. In this case, it renders the JSX element into the HTML element with the ID root.
Why Use React?
Hundreds of major companies worldwide, such as Netflix, Airbnb, and American Express, use React to build their web apps. In this section, we’ll discuss the reasons why many developers choose React over its counterparts.
1. Easy to Use
Web developers with JavaScript knowledge can learn how to use React quickly since it relies on plain JavaScript and follows a component-based approach. With it, you can develop web-based applications.
If you need to become more familiar with JavaScript, many websites provide free coding lessons. Once you know the basics of JavaScript, read up on React to streamline the front-end development process.
2. Supports Reusable Components
React lets you reuse components that have been developed for other applications. As such, you can pre-build React components to significantly reduce the development time for complex web applications.
Furthermore, React allows you to nest components within others to create complex functions without bloating the code. Each React component has its controls, making maintenance seamless.
3. Easier Component Writing
React enables you to combine JavaScript objects with HTML syntax and tags. You can also write components and add React to an existing HTML page with JSX integration. JSX simplifies multi-function rendering, keeping the code concise without limiting the app’s capabilities.
Even though JSX is not the most widely used syntax extension, it has proven to be efficient in special component and dynamic application development.
React’s official command-line interface (CLI) tool, called Create React App, further streamlines the development of single-page applications. It features a modern build setup process with pre-configured tools that are excellent for learning React.
4. High Performance
The Virtual Document Object Model allows React to update the DOM tree efficiently. By storing Virtual DOM in memory, React eliminates excessive re-rendering that could harm performance.
Additionally, React’s one-way data binding between elements streamlines the debugging process. Any modifications to child components won’t affect the parent structure, reducing the risk of errors.
5. SEO Friendly
React can improve web applications’ search engine optimization (SEO) by enhancing their performance and reducing the time it takes for JavaScript code loads. Virtual DOM implementation contributes to faster page speeds.
It also helps search engines navigate web applications by enabling server-side rendering. This type of rendering addresses one of the most significant challenges JavaScript-heavy websites face, as search engines typically find them difficult and time-consuming to crawl.
React Features
React.js features and functionality distinguish it from other JavaScript libraries. The following sections will introduce these features and explain their contribution to mobile and web application development.
1. JSX
JSX is a JavaScript syntax extension used in React for element creation. Developers employ it to embed HTML code within JavaScript objects. JSX can embed JavaScript functions and expressions, simplifying complex code structures.
Let’s look at an example of embedding an expression in JSX:
const name = 'John Smith'; const element = <h1>Hello, {name}</h1>; ReactDOM.render( element, document.getElementById('root') );
In the second line, the name variable is embedded using curly brackets. This allows dynamic content to be included in the JSX. Meanwhile, the ReactDOM.render() function, defining the user interface, renders the React element on the DOM tree.
JSX also helps mitigate Cross-Site Scripting (XSS) attacks. By default, React DOM converts values embedded in JSX to strings before rendering them. As a result, third parties cannot inject additional code through user input unless explicitly specified in the application.
2. Virtual DOM
The Document Object Model (DOM) represents a web page as a structured data tree. React stores Virtual DOM trees in memory, allowing it to efficiently update specific data tree parts faster than re-rendering the entire DOM tree.
Whenever there’s a change in data, React generates a new Virtual DOM tree and compares it to the previous one to determine the most efficient way to implement changes in the real DOM. This process is known as diffing.
Ensuring that user interface manipulation affects only specific sections of the real DOM tree makes the updated version quicker and consumes fewer resources. This practice significantly benefits large projects with intensive user interaction.
3. Components and Props
React divides the UI into isolated, reusable pieces of code called components. React components are the foundation for creating user interfaces. They function similarly to JavaScript functions, accepting arbitrary inputs known as properties or props.
Returned React elements, generated when you render interactive React components, are crucial in defining how the user interface will appear on the client’s end. Here’s an example of a React component:
function Welcome(props) { return <h1>Hello, {props.name}</h1>; }
Interactive components, whether class components or function components, receive data through properties or props from the parent component. React also supports asynchronous components, enabling efficient data fetching and rendering.
You can have as many React components as needed without cluttering your code.
4. State Management
A state is a JavaScript object holding a React component’s data. This state can change when a user interacts with the application, rendering a new user interface that reflects these modifications.
State management can be handled within React itself or optionally through third-party libraries. They facilitate more complex data handling and trigger the re-rendering process whenever the data changes.
Two of the most popular state management libraries are Redux and Recoil.
Redux
The Redux state management library offers a centralized store that maintains the state tree of an application, ensuring predictability. Redux’s architecture supports error logging for easier debugging and follows a strict code organization method.
However, Redux can be complex and may not be the best choice for small applications with a single data source.
Recoil
Recoil employs pure functions called selectors to calculate data from updateable units of the state known as atoms. Multiple components can subscribe to the same atom, enabling them to share a state.
This approach prevents redundant states, simplifies code, and eliminates excessive re-renders of React and its child components. Recoil is often considered more beginner-friendly than Redux due to its simpler core concepts.
5. Programmatic Navigation
Programmatic navigation refers to situations where lines of code trigger actions that redirect users. For example, when users perform login or sign-up actions, programmatic navigation takes them to the relevant pages.
React Router, React’s standard library for routing, offers multiple ways to achieve safe programmatic navigation between components without requiring users to click links.
The primary method of programmatic navigation is using a Redirect component, but you can also utilize history.push() as an alternative.
In short, the React Router package syncs the UI with the URL, granting you control over the appearance of React applications without relying solely on user-initiated link clicks.
Understanding React Native
React Native is an open-source JavaScript framework built on the React library. Developers use it to create cross-platform React applications for iOS and Android, providing a seamless experience for native user interfaces.
React Native leverages native application programming interfaces (APIs) to render mobile UI components in Objective-C (iOS) or Java (Android). This approach allows developers to create platform-specific components while sharing source code across multiple platforms.
Despite their similarities, React Native differs from React.js. Here’s a quick comparison of React.js vs React Native:
Aspect | React.js | React Native |
Platform | Web applications | Mobile applications (iOS, Android, etc.) |
Rendering | DOM-based | Native components |
Language | JavaScript | JavaScript |
Development Approach | Single-page applications | Cross-platform mobile apps |
UI Components | HTML, CSS, and React components | Native components and React components |
Navigation | Browser-based navigation | Native navigation |
Third-Party Plugins | Web-specific extensions, plugins, and libraries | Native mobile plugins and libraries |
Performance | Web performance | Native-level performance |
User Experience | Web-based UI | Native look and feel |
How to Use React?
Implementing web development with React involves various steps and best practices to ensure efficient and maintainable code. Follow these steps to get started with React:
1. Check Node.js and npm Installation
Before working with React, install Node.js and npm (Node Package Manager) on your system. You can verify their installation by running the following commands in your terminal:
node -v npm -v
If Node.js and npm are not installed, you can download and install them from the official Node.js website.
2. Create React App
React offers a convenient tool called Create React App that simplifies project setup. To create a new React app, execute the following command:
npx create-react-app my-react-app
Replace my-react-app with your desired application name. This command creates a new React project with the necessary files and dependencies.
3. Navigate to Your Project Directory
Access your project directory using the following command:
cd my-react-app
4. Start the Development Server
Launch the development server to view your React app in action:
npm start
This command initiates the development server, making your React app accessible at http://localhost:3000. The development server also provides live reloading, allowing you to observe real-time changes as you edit your code.
5. Explore the Project Structure
Take a moment to explore the project structure created by the Create React App. Key directories include:
- src – this folder contains your application’s source code.
- public – this folder includes static assets and the HTML template for your app.
6. Create Your First React Component
Open the src directory and navigate to the App.js file. This file serves as the entry point of your React app. Modify the contents of this file to create your first React component.
Here’s a basic example of a functional component:
import React from 'react'; function App() { return ( <div> <h1>Hello, React!</h1> </div> ); } export default App;
7. Render Your Component
React components receive data and determine what should appear on the client’s side. To display your component, render it in the src/index.js file:
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render(<App />, document.getElementById('root'));
8. Start Building Your App
With your first React component ready, you can continue to build your React application. Create additional components, handle user interactions, and manage states as your project evolves.
How to Deploy a React Application on Hostinger?
To make your React application accessible to users on the web, you need to deploy it on a server. Hostinger’s VPS hosting comes with full root access, a comprehensive File Manager, and an AI assistant. It is also easily scalable, making it an excellent choice for hosting your React projects.
The tutorial below will demonstrate how to deploy a React app on Hostinger with an Ubuntu 22.04 64bit operating system:
- Ensure your React app is production-ready by creating a production build. Navigate to your project directory that contains your React app and run the following command:
npm run build
- Log in to your Hostinger VPS using SSH. Open the terminal and type the command below, replacing your_server_ip with your actual VPS IP server:
ssh root@your_server_ip
- Next, make sure your VPS is up to date by executing these commands:
sudo apt update sudo apt upgrade
- Upload the contents of your build directory to your VPS. Replace path/to/your/app with the local path to your build directory and your-username@your-server-ip:/path/to/destination with the appropriate remote path on your VPS.
scp -r path/to/your/app your-username@your-server-ip:/path/to/destination
- Install Node.js and NGINX on your VPS using the command below. Note that this action will also remove Apache2 if it’s installed.
sudo apt-get purge apache2* curl -sL https://deb.nodesource.com/setup_18.x -o nodesource_setup.sh sudo bash nodesource_setup.sh sudo apt install -y nodejs nginx
- Create an NGINX configuration file for your app. Ensure to replace your-app with a unique and descriptive name:
sudo nano /etc/nginx/sites-available/your-app
- Add the following configuration, replacing your-domain-or-ip with your actual domain name or IP address and your-port with the port your app is running on:
server { listen 80; server_name your-domain-or-ip; location / { proxy_pass http://127.0.0.1:your-port; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
- Save and close the file. Then, create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled/
- Test the NGINX configuration for syntax errors using the following command:
sudo nginx -t
- If there are no errors, restart NGINX:
sudo service nginx restart
Your React app is now live and accessible through your domain name or VPS IP address.
Conclusion
React is a robust JavaScript library that empowers web developers to create dynamic and efficient web applications. It simplifies JavaScript coding and enhances performance, making it a top choice for front-end development.
Here’s a recap of why you should consider using React to create high-performance web applications:
- React’s simplicity and abundant online coding resources make it accessible to developers of all levels.
- React supports component reusability, significantly reducing development time and effort.
- JSX facilitates coding and element rendering, improving code readability and maintainability.
- The React Virtual DOM eliminates unnecessary re-rendering, ensuring your application runs smoothly and efficiently.
- React uses server-side rendering that improves web app pages’ load time, positively impacting SEO.
React has greatly benefited from continuous improvements by the React team. It seamlessly integrates with various technologies, including Bootstrap, Tailwind CSS, Axios, Redux, and Firebase. Deploying a React application on a VPS is also straightforward with the assistance of Node.js and NGINX.
If you have any questions or insights about React, feel free to leave them in the comments section below.
What Is React FAQ
This section will answer the most common questions about React.
Is React a Framework or a Programming Language?
React is not a framework or programming language but a JavaScript library. React focuses on building user interfaces and is often used in conjunction with other technologies and libraries to create complete web or mobile applications. It provides a component-based approach to UI development, making it a valuable tool for front-end developers.
Is React a Front-End or a Back-End Framework?
React is a front-end library, not a back-end framework. It is used to create user interfaces and is typically employed on the client side of web applications. React works with back-end technologies and can communicate with server-side code to fetch data. Still, its primary role is to handle the presentation layer of applications on the user’s device.
Which Programming Language Is Used for React?
React itself is primarily written in JavaScript. Therefore, to work effectively with React, developers should understand JavaScript well, including its syntax, website terminology, concepts, and features. Additionally, JSX, a syntax extension often used with React, combines HTML-like tags with JavaScript to define React components.
Is React Easy to Learn?
Learning React coding is relatively easy for developers with JavaScript experience, thanks to its component-based architecture and straightforward concepts. Numerous React docs, tutorials, and the support of the React community can assist developers in mastering React quickly. However, the learning curve can vary based on the individual’s prior programming knowledge.
Comments
June 03 2022
The article is incomplete. It talks about how to proceed if there is a VPS, but it doesn't make it clear what to do when there isn't.
June 07 2022
Hi there! The VPS and node.js mention is just a suggestion. If you're running your website on a shared or cloud plan, you will not be able to use node.js, but you can still use React with just JS - you can find more about it here ?
March 25 2024
Thanks for your tutorial! I built the react app locally on my PC. Where should I place files from the build folder?
April 18 2024
Hello Nadzeya! You can upload your files to your preferred directory, ideally before running the npm build command on your PC. Then, navigate to that directory in the terminal and run npm run build. After that, you can follow the rest of the guide for deploying React apps 😊