For working professionals
For fresh graduates
More
Imagine building a house from scratch. Instead of starting with individual tasks, you are handed pre-made walls, windows, and doors. That is React JS in a nutshell. Developed by Facebook, React revolutionized web development by introducing a component-based architecture. Like assembling a house, React allows you to piece together reusable UI components resulting in the construction of intricate web applications. With React, you can focus on crafting engaging user experiences without worrying about DOM manipulation.
React.js is a popular JavaScript library that can be used to build user interfaces. It is particularly for single-page applications. React is frequently adopted because of its efficiency, simplicity, and strength. In this react js tutorial, we will cover details including the information you need to know to get started with React.js, from basic to advanced.
React.js offers several benefits, including:
Given below is a step-by-step react tutorial for creating a React app through Create React App, a popular tool for setting up React projects quickly.
Before starting, ensure you have Node.js and npm installed on your system. You can download and install them from the official website: Node.js.
Open your terminal or command prompt and install Create React App using npm:
npm install -g create-react-app
Once Create React App is installed, you can create a new React app with a single command:
npx create-react-app my-react-app
Replace my-react-app with the name you want for your project. This command will set up a new React project with all the necessary files and dependencies.
Navigate into your newly created React app directory:
cd my-react-app
Start the development server to see your React app in action:
npm start
This command will start a development server and open your default web browser to display your React app. By default, it runs on http://localhost:3000.
Once the development server runs, you can explore the project structure and make changes to your app. The main files and directories you will work with are
src/: This directory contains your application's source code.
public/: This directory contains static assets like HTML files and images.
package.json: This file manages your project's dependencies and scripts.
Now that your React app is up and running, you can start making changes and customization. You can edit the files in the src/ directory, such as App.js, to modify the content and components of your app.
When you are ready to deploy your React app to production, you can create a production build using the following command:
npm run build
This command creates an optimized build of your app in the build/ directory, which can then be deployed to a web server.
To deploy your React app to a web server, you can upload the contents of the build/ directory to your hosting provider using FTP, or you can use platforms like Netlify, Vercel, or GitHub Pages for easy deployment.
When learning what is react js, you will come across components. In React, components are the building blocks of your application's user interface. A component is a reusable piece of UI that encapsulates a specific piece of functionality or behavior. There are two main types of components in React:
Functional components are JavaScript functions that accept props (short for properties) as arguments and return React elements. They are simple and easy to understand, making them a popular choice for defining UI elements.
Example of a functional component:
import React from 'react';
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
export default Welcome;
Class components are ES6 classes that extend the React.Component class. They have additional features such as state and lifecycle methods. Class components are useful for managing stateful behavior and complex logic.
Example of a class component:
import React, { Component } from 'react';
class Welcome extends Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
export default Welcome;
In React, rendering refers to the process of transforming React components into DOM elements and displaying them on the screen. There are two primary methods for rendering components:
Example:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
Example:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
export default Counter;
Composing components in React involves building UIs by combining multiple smaller components. This allows for better code organization, reusability, and maintainability. Here is how to compose components:
Example:
import React from 'react';
import Header from './Header';
import Footer from './Footer';
import Content from './Content';
function App() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
);
}
export default App;
Example:
import React, { Component } from 'react';
import Header from './Header';
import Footer from './Footer';
import Content from './Content';
class App extends Component {
render() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
);
}
}
export default App;
Extracting components involves breaking down a complex component into smaller and reusable components. This improves code readability, facilitates component reuse, and simplifies maintenance. Here is how to extract components:
Props (short for properties) pass data from parent components to child components in React. Props are immutable and are passed down the component tree in a unidirectional flow. Here is a react tutorial for beginners on how props work:
You can pass props to a component by adding attributes to it when you use it in JSX.
Example:
// Parent Component
import React from 'react';
import Welcome from './Welcome';
function App() {
return <Welcome name="John" />;
}
export default App;
Functional components receive props as a parameter, while class components access props via this.props.
Example:
// Functional Component
import React from 'react';
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Class Component
import React, { Component } from 'react';
class Welcome extends Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
You can use props to customize the behavior and appearance of a component based on the data passed from its parent.
Example:
import React from 'react';
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
function App() {
return (
<div>
<Welcome name="John" />
<Welcome name="Jane" />
</div>
);
}
export default App;
You can define default values for props using the defaultProps property in a component.
Example:
import React from 'react';
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
Welcome.defaultProps = {
name: 'Guest'
};
export default Welcome;
Props allow you to create reusable and customizable components in React. They enable you to pass data and behavior between components, which makes your application better and modular. If you understand the details and how to use props, it will be easier to build complex React applications.
React JS is at a rising high in the world of web development, as it holds a powerful toolkit to build efficient, scalable, and maintainable user interfaces. If you want to use a component-based architecture, virtual DOM, and provide a rich ecosystem of libraries and tools, React is now a new way we approach building web applications. From its intuitive syntax to its ability to handle complex state management, React creates a dynamic experience for users and helps developers.
Mukesh Kumar
Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management and Pro…Read More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.