No history yet

Introduction to ReactJS

What is React?

React is a JavaScript library for building user interfaces. Created by Facebook, its main job is to make it easier to create interactive, dynamic UIs for web applications. The core idea is simple but powerful: you build your interface out of small, reusable pieces of code called components.

Think of it like building with LEGO bricks. Instead of constructing a whole castle from a single, massive block of plastic, you use individual bricks. You can reuse the same type of brick in different parts of your castle, and if you need to change one part, you only have to swap out a few bricks, not rebuild the entire thing.

In React, these "bricks" are components. A button, a search bar, a user profile card—each can be its own component. You then assemble these components to create your final application.

React, a vital tool in modern web development demands an organized approach for enhanced readability and maintainability.

Why Use React?

Besides its component-based structure, React has another major benefit: performance. It achieves this with something called the Virtual DOM.

Web browsers use a structure called the Document Object Model (DOM) to represent a webpage. Updating the DOM is slow. When you change something on a page, the browser has to do a lot of work to repaint the screen. React speeds this up by keeping a lightweight copy of the DOM in memory—the Virtual DOM.

When you make a change, React first updates its fast, virtual version. Then, it compares the new Virtual DOM with the old one, figures out the most efficient way to update the real DOM, and applies only the necessary changes. It's like having a blueprint of your house. Instead of demolishing and rebuilding the whole structure to change a window, you just update the blueprint and tell the workers exactly which window to replace.

Setting Up Your Workspace

Before you can start building with React, you need two things: Node.js and a code editor.

1. Node.js: This is a JavaScript runtime environment. It lets you run JavaScript on your computer, outside of a web browser. It also comes with npm (Node Package Manager), a tool for installing and managing the various libraries and packages your project will depend on.

2. Code Editor: Any text editor works, but a dedicated code editor like Visual Studio Code (VS Code) provides helpful features like syntax highlighting, code completion, and an integrated terminal. It's free and highly recommended.

Lesson image

Once you have Node.js and a code editor installed, you can create a new React application from your terminal. We'll use a tool called Vite, which sets up a fast, modern development environment for you. Open your terminal and run these commands one by one:

# 1. Create a new project using Vite
npm create vite@latest my-react-app -- --template react

# 2. Navigate into your new project directory
cd my-react-app

# 3. Install the necessary packages
npm install

# 4. Start the development server
npm run dev

After running npm run dev, you'll see a local server address (usually http://localhost:5173). Open that link in your browser to see your brand new React application running.

A First Look at JSX

When you open the project files (look inside the src folder), you'll see something that looks like a mix of HTML and JavaScript. This is JSX, which stands for JavaScript XML. It's a syntax extension that lets you write HTML-like code directly inside your JavaScript files.

While it might look strange at first, JSX makes it much more intuitive to describe what your UI should look like. You're not writing HTML in a string; you're creating JavaScript objects that React uses to build the DOM.

Here's a basic example of a React component using JSX:

function Greeting() {
  const userName = 'Alice';

  return (
    <div className="greeting-card">
      <h1>Hello, {userName}!</h1>
      <p>Welcome to your first React component.</p>
    </div>
  );
}

Notice a couple of key things:

  • className instead of class: Since class is a reserved word in JavaScript for creating classes, JSX uses className for applying CSS classes.
  • Curly Braces {}: This is the magic syntax for embedding JavaScript expressions directly into your markup. Here, we're inserting the value of the userName variable into the <h1> tag.

This combination of markup and logic in one place is a hallmark of React. It keeps related code together, making components self-contained and easy to understand.

Now that you understand the basic concepts, let's check your knowledge.

Quiz Questions 1/5

What is the primary purpose of the React library?

Quiz Questions 2/5

How does React's Virtual DOM improve application performance?

You've taken the first step into the world of React. Next, we'll dive deeper into creating and combining components to build more complex interfaces.