No history yet

Introduction to React

What Is React?

React is a JavaScript library for building user interfaces. Created by Facebook, it's not a full framework but a focused tool for creating the view layer of an application—what the user actually sees and interacts with in their browser.

Imagine building a complex website with traditional JavaScript. When a piece of data changes, you have to manually find the corresponding part of the webpage and update it. If you have a social media feed, a notification counter, and a chat window all on the same page, this can get complicated quickly. A change in one area might require updates in several others.

React simplifies this process. You tell React what you want the user interface to look like for any given state of your application, and it efficiently updates and renders just the right components when your data changes.

Lesson image

Why Use React?

The main advantage of React is its efficiency. Traditionally, when data on a webpage changes, the entire page needs to be re-rendered in the browser's Document Object Model (DOM). This can be slow and clunky, especially for complex applications.

React introduces a concept called the Virtual DOM. Think of it as a lightweight copy of the real DOM. When the application's state changes, React first updates this Virtual DOM. Then, it compares the Virtual DOM with the real DOM, figures out the smallest possible changes needed, and applies only those changes. This process, called reconciliation, makes applications feel much faster and more responsive.

Instead of rebuilding the entire house when you want to change the color of a door, React just repaints the door.

Another key benefit is its component-based structure. It allows you to break down a complex user interface into smaller, isolated, and reusable pieces of code. We'll dive deeper into components later, but for now, just know that this approach makes your code cleaner, easier to manage, and simpler to debug.

Setting Up Your Environment

To get started with React, you need two things installed on your computer: Node.js and its package manager, npm. Node.js lets you run JavaScript outside of a web browser, and npm is used to manage project dependencies, including React itself. If you don't have them, you can download them from the official Node.js website.

The easiest way to create a new React application is with a tool called Create React App. It sets up a new project with a sensible default configuration, so you can focus on writing code instead of wrestling with build tools.

Open your terminal or command prompt and run the following command to create a new app called my-first-app:

npx create-react-app my-first-app

This might take a few minutes. Once it's done, navigate into your new project directory and start the development server:

cd my-first-app
npm start

Your web browser will automatically open to http://localhost:3000, and you'll see the default React welcome page. Congratulations, you've just run your first React application!

The Basic Structure

Create React App generates a standard folder structure. For now, we only need to focus on a few key files.

  1. public/index.html: This is the only HTML page in your application. It's the shell of your app. If you look inside, you'll find a <body> tag with a single <div> inside it: <div id="root"></div>. This div is the container where your entire React application will be rendered.

  2. src/index.js: This is the main JavaScript entry point. Its job is to find that <div id="root"> from index.html and inject our React code into it. It's the bridge between the browser's DOM and our React application.

  3. src/App.js: This is the root component of your application. Think of it as the main container for all other components you will build. This is where you'll spend most of your time writing your application's user interface.

Now you're ready to test your knowledge.

Quiz Questions 1/6

What is the primary role of React in web development?

Quiz Questions 2/6

What problem does the Virtual DOM primarily solve?

You now have a foundational understanding of what React is, why it's useful, and how to set up a basic project. In the next section, we'll start modifying the App.js file and learn about JSX, the syntax that lets us write HTML-like code in our JavaScript.