No history yet

Introduction to React

What Is React?

React is a JavaScript library for building user interfaces. Think of it like a set of tools that makes creating interactive websites and applications much easier. Instead of building a whole webpage as one giant piece, React encourages you to break it down into smaller, reusable parts called components.

Imagine building a house with LEGO bricks. You don't create the whole house from a single block. Instead, you use individual bricks—a window brick, a door brick, a wall brick. You can reuse these bricks to build different structures. Components in React work the same way. A button, a search bar, or a user profile card can each be a component. You build these small, independent pieces and then assemble them to create a complex application.

The core idea is simple: build small, self-contained components and snap them together to create your user interface.

Writing with JSX

When you look at React code, you'll see something that looks a lot like HTML mixed right into the JavaScript. This is called JSX, which stands for JavaScript XML. It's a syntax extension that lets us write HTML-like code inside our JavaScript files.

For example, instead of creating an element using complex JavaScript functions, you can just write it like you would in an HTML file. This makes the code much more readable and intuitive. Here’s how you’d create a simple heading:

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

Behind the scenes, a tool like Babel transpiles this JSX into regular JavaScript that browsers can understand. It might look a little strange at first, but it quickly becomes a powerful way to describe what your UI should look like.

Components, Props, and State

Components are the heart of React. They are independent, reusable pieces of code. A component is like a JavaScript function that returns a piece of the user interface. There are two main ways to define a component, but the simplest is a JavaScript function.

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

Notice the props in the function above? Props (short for properties) are how components talk to each other. They allow you to pass data from a parent component down to a child component. Props are read-only, meaning a component should never change its own props.

What if a component needs to manage its own data that changes over time, like a user typing into a form or a counter that increments? For this, we use state.

State is data that is managed within a component. When the state of a component changes, React automatically re-renders the component to reflect the new state. Unlike props, state is private and fully controlled by the component.

FeaturePropsState
PurposePass data from parent to childManage a component's internal data
MutabilityImmutable (read-only)Mutable (can be changed)
SourcePassed down from a parent componentManaged and updated by the component itself
ExampleA User component receives a username propA Counter component keeps track of the current count in its state

The Virtual DOM

Web browsers use something called the Document Object Model (DOM) to represent the structure of a webpage. When you want to change something on the page, like the text in a button, you have to directly manipulate the DOM. This can be slow and inefficient, especially in complex applications where many things are changing at once.

React solves this problem with the Virtual DOM. The Virtual DOM is a lightweight copy of the actual DOM, kept in memory. Instead of updating the real DOM every time something changes, React takes a different approach.

When a component's state changes, React first updates the Virtual DOM. Then, it compares this new version of the Virtual DOM with the previous one, in a process called "diffing." It identifies exactly what has changed and calculates the most efficient way to update the real DOM. Finally, it updates only those specific parts of the real DOM, rather than re-rendering the entire page.

This process is incredibly fast and is one of the key reasons for React's high performance.

Component Lifecycle

Every React component goes through a lifecycle of events: it gets created and added to the DOM, it updates when its props or state change, and finally, it gets removed from the DOM.

React provides special methods, called lifecycle methods, that allow you to run code at specific points in this cycle. These are more common in class-based components. Here are the three main phases:

  1. Mounting: This is when an instance of a component is being created and inserted into the DOM. Methods like constructor() and componentDidMount() are called during this phase.
  2. Updating: An update can be caused by changes to props or state. The component re-renders, and methods like shouldComponentUpdate() and componentDidUpdate() are executed.
  3. Unmounting: This is the final phase, where the component is removed from the DOM. The componentWillUnmount() method is called right before this happens, allowing for any necessary cleanup.

Understanding the component lifecycle is crucial for managing tasks like fetching data from an API when a component first appears or cleaning up resources when it disappears.

Quiz Questions 1/5

What is the primary concept behind React's approach to building user interfaces?

Quiz Questions 2/5

What is JSX?

These concepts—components, JSX, props, state, and the Virtual DOM—are the fundamental building blocks of React. Mastering them is the first step toward building powerful, interactive web applications.