No history yet

Introduction to React

What is React?

React is a JavaScript library for building user interfaces. Think of a user interface (UI) as everything you see and interact with on a webpage—the buttons, the text fields, the images, and the menus. React helps developers build these UIs in a structured and efficient way.

Created and maintained by Facebook (now Meta), React’s main job is to make it painless to create interactive UIs. You design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes. This declarative approach makes your code more predictable and easier to debug.

React is a JavaScript library for building user interfaces.

The Core Ideas

React is built on a few powerful concepts that make it so effective. Understanding them is key to understanding React itself.

Component-Based Architecture

Instead of building a massive, single piece of code for a webpage, React encourages you to break the UI into small, independent, and reusable pieces called components. A component is like a LEGO brick. You can build a simple component, like a button, and then reuse it anywhere you need a button. You can also combine small components to create larger ones, just like assembling LEGOs to build a castle.

This approach makes your code much easier to manage. If you need to change how the like button works, you only have to edit the LikeButton component, and the change will apply everywhere it's used.

The Virtual DOM

Updating what you see on a webpage (the Document Object Model, or DOM) can be slow. If you have a list of 1,000 items and one changes, you don't want the browser to re-render the entire list. React solves this with a clever trick: the Virtual DOM.

The Virtual DOM is a lightweight copy of the real DOM that exists only in memory. When a component's data changes, React first creates a new Virtual DOM. Then, it compares this new version with the previous one, figuring out the exact minimum changes needed. This process is called "diffing."

Finally, React takes these calculated changes and applies them to the real DOM in one efficient batch. It’s like having a blueprint for a building. Instead of tearing down the whole structure to change a window, you update the blueprint, see what’s different, and then tell the construction crew to only replace that one window.

Unidirectional Data Flow

In React, data has a one-way street. It flows downwards from parent components to child components. This is known as unidirectional data flow.

A parent component can pass data to its children, but a child component cannot directly change the data of its parent. This might sound restrictive, but it makes the application incredibly predictable. When something goes wrong, you can trace the flow of data downwards to pinpoint the source of the issue, rather than searching everywhere for what might have caused the change.

Lesson image

React vs Other Frameworks

So where does React fit in the world of web development? It's often compared to frameworks like Angular and Vue. The main difference is one of scope.

FrameworkTypePhilosophy
ReactLibraryFocuses only on the UI layer. It's unopinionated, giving you the freedom to choose other libraries for things like routing and state management.
AngularFull FrameworkA comprehensive solution that includes everything you need to build an application, from the UI to data fetching and state management. It's more structured and opinionated.
VueProgressive FrameworkSits somewhere in the middle. It offers a core library for the view layer but also has an ecosystem of supporting libraries that are officially maintained.

Choosing one over the other often comes down to the project's needs and the team's preference. React's flexibility and massive community have made it an extremely popular choice for projects of all sizes.

Getting Started

To start building with React, you'll need two things on your computer: Node.js and npm (Node Package Manager). Npm comes bundled with Node.js, so you only need one installation.

Node.js is a JavaScript runtime that lets you run JavaScript outside of a web browser, and npm is a tool for managing the various libraries (like React) your project will depend on.

Once you have those installed, the simplest way to create a new React application is by using a tool called Create React App. It sets up a complete development environment for you with a single command, so you can start writing components right away without worrying about complex configuration.

# This command sets up a new React project in a folder called 'my-app'.
npx create-react-app my-app

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

# Start the development server.
npm start

This setup gives you a live-reloading development server, which means any changes you save in your code will automatically appear in the browser. It's a great way to get a feel for how React works.

Quiz Questions 1/5

What is the primary purpose of the React library?

Quiz Questions 2/5

To start a new React project, what two tools are essential prerequisites to have installed on your computer?

Now you have a grasp of what React is and the core ideas behind it. It's a powerful tool for building modern, interactive user interfaces.