No history yet

React Basics

What Is React?

React is a JavaScript library for building user interfaces. Think of it as a toolkit for creating the visual parts of a website or app that users interact with. Without a library like React, you'd have to manually write a lot of JavaScript code to update what's on the screen every time something changes. This can get complicated and slow.

React simplifies this process. Instead of telling the browser exactly how to change the page step-by-step, you just tell React what you want the page to look like based on the current data. React then figures out the most efficient way to make it happen. It does this by breaking down the user interface into small, reusable pieces.

Components are the fundamental building blocks of any React application.

These pieces are called components. A button, a search bar, or an entire user profile card can all be components. You can build simple components, then combine them to create more complex ones, much like building with LEGO bricks. This makes your code more organized, easier to manage, and reusable across your application.

Lesson image

Setting Up Your First Project

The fastest way to start a new React project is with a tool called Create React App. It sets up a complete development environment for you with just a few commands. You'll need to have Node.js and its package manager, npm, installed on your computer first. Most developers already have these, but if you don't, you can download them from the official Node.js website.

Once Node.js is ready, open your terminal or command prompt and run the following command. This will create a new folder called my-first-app with everything you need to get started.

npx create-react-app my-first-app

After the process completes, navigate into your new project directory and start the development server.

cd my-first-app
npm start

This will open a new tab in your web browser with your new React application running. Any changes you make to the source code will now automatically update in the browser.

The Virtual DOM

One of React's key performance features is its use of a Virtual DOM. The DOM, or Document Object Model, is the browser's representation of your webpage's structure. Directly changing the real DOM is slow and resource-intensive.

React gets around this by keeping a lightweight copy of the DOM in memory, called the Virtual DOM. When the data in your application changes, React first updates this virtual copy. Then, it compares the new Virtual DOM with a snapshot of the old one to see exactly what changed. This process is called "diffing."

Finally, React takes all the calculated changes and updates the real DOM in one single, efficient batch. This avoids unnecessary redraws and makes the application feel much faster and more responsive.

Writing with JSX

When you look at React code, you'll see something that looks like HTML right inside the JavaScript. This is JSX, which stands for JavaScript XML. It's a syntax extension that makes writing React components much more intuitive.

For example, instead of creating elements with complex JavaScript functions, you can just write this:

const greeting = <h1>Hello, world!</h1>;

While it looks like HTML, it's actually JavaScript. You can embed JavaScript expressions directly inside JSX by wrapping them in curly braces {}. This makes it easy to display dynamic data.

const name = 'Alice';
const greeting = <p>Hello, {name}!</p>;

Your browser doesn't understand JSX directly. A tool called a transpiler, like Babel, runs behind the scenes to convert your JSX into regular JavaScript that browsers can execute.

Your First Component

Components are the core of React. They are essentially JavaScript functions that return JSX, describing a piece of the user interface. By convention, component names always start with a capital letter.

Here’s what a very simple component looks like. This function, Welcome, returns a heading element.

function Welcome() {
  return <h1>Hello from the Welcome component!</h1>;
}

Once you define a component, you can use it just like a regular HTML tag. You can also compose components, meaning you can use components inside other components. This is how you build a complex UI from small, manageable parts.

For instance, we can create an App component that uses our Welcome component multiple times.

function Welcome() {
  return <h1>Hello from the Welcome component!</h1>;
}

function App() {
  return (
    <div>
      <Welcome />
      <Welcome />
      <Welcome />
    </div>
  );
}

This ability to build, nest, and reuse components is what makes React so powerful for creating modern web applications.

Quiz Questions 1/6

What is the primary purpose of the React library?

Quiz Questions 2/6

How does React's Virtual DOM improve application performance?

Now that you've got the fundamentals down, you're ready to start building more complex interfaces with React.