Lesson 1 / 119%

Lesson 1

Introduction to React

10 min

Understand what React is, the idea of components and what you need to know before starting.

Definition

React is an open-sourceJavaScript librarythat allows you to build user interfaces from small, independent, reusable blocks called components. Rather than directly modifying the elements of a page, you describe what the user should see based on the current data. React then takes care of updating the interface: this is a declarativeapproach.

Welcome to this introductory React course. Before writing code, it is important to understand what this library is, how it works, and why it is used in so many web applications.

This first lesson will give you a general overview of React. You will discover components, the declarative approach, the advantages of this technology, and the knowledge required to start using it. The concepts covered here will serve as a foundation for the following lessons.

What Is React?

React is an open-sourceJavaScript libraryoriginally developed at Facebook, now Meta. It allows you to create interactive user interfacesfor websites and applications. It is particularly suitable for interfaces that change frequently in response to user actions.

A user interface includes everything you see and interact with in an application: buttons, menus, forms, lists, cards, and the different sections of a page. React allows you to organize these elements in a clear and reusable way.

For example, in an online store, React can display products, update the shopping cart when you add an item, and automatically change the total amount. In a task management application, it can add or remove items without requiring a complete page reload.

A Library, Not a Complete Framework

React is generally described as a libraryrather than a complete framework. This means that it focuses mainly on creating and updating the user interface. For other features, such as navigation or certain server operations, you can use additional tools.

This approach offers great flexibility. You can start with a simple application, then gradually add the tools you need. React also integrates with different environments and can be used in projects of many different sizes.

  • React:builds components and updates the interface.
  • Vite:makes it easier to create and develop a React project.
  • React Router:helps organize navigation between different pages.
  • Next.js:provides a complete framework built around React.

The Core Idea: Components

With React, an interface is divided into small blocks called components. A component can represent a button, a card, a form, a navigation bar, or even an entire page. These components can then be assembled to build a complete application.

Imagine a set of building blocks: each piece has a specific purpose and can be reused several times. AButtoncomponent can appear in different places with different text or actions, without having to rewrite its entire structure.

This organization makes code maintenance easier. If you change the appearance of a component reused across several pages, the changes can be applied everywhere that component is used. This helps maintain a consistent interface and reduces repetition.

Diagram of a web page divided into components: App, Header, List, Card, and Footer, along with their component tree.

Understanding the Component Tree

React components are organized into a hierarchical structure called a component tree. A main component can contain several child components, which can themselves contain other components.

example of a component tree
App
├── Header
│   ├── Logo
│   └── Navigation
├── ProductList
│   ├── ProductCard
│   ├── ProductCard
│   └── ProductCard
└── Footer

In this example, the main componentAppcontains the header, a list of products, and the footer. Each product card can be built from the same component while displaying different information.

What Does a React Component Look Like?

A simple React component can be written as a JavaScript function. This function returns a description of the interface to display, usually written using a syntax called JSX.

example of a React component
function Welcome() {
  return <h1>Hello!</h1>;
}

export default Welcome;

Here, theWelcomecomponent simply displays a heading. Although the code looks like HTML, it is actually JSX, a syntax used with JavaScript. You will learn to use it in more detail in the following lessons.

A Declarative Approach

In plain JavaScript, you can directly modify the elements of a page: find an element, change its text, or add a CSS class. With React, you instead describewhat the interface should displaybased on the available data.

This way of programming is called adeclarative approach. You define the expected result, and React takes care of updating the necessary parts when the data changes. You therefore do not need to manually manipulate every element on the page.

Example: A Click Counter

Imagine a button that increases a counter with every click. In plain JavaScript, you can find the element containing the number and directly change its content. With React, you store the counter value in data calledstate.

React counter example
import { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <button onClick={() => setCount(count + 1)}>
      Clicks: {count}
    </button>
  );
}

export default Counter;

In this example,useStatestores the counter value. When the user clicks, the value increases and React automatically updates the display. You do not need to find the button to change its text.

You do not need to understand every line of this example immediately. Simply remember that React connects data and the display. When the data changes, the interface reflects its new value.

Understanding Dynamic Interfaces

A dynamic interface is an interface that can change in response to user actions or new data. It does more than display static content.

  • A counter:the displayed number increases after each click.
  • A form:the information entered can be displayed or validated.
  • A task list:items can be added, edited, or deleted.
  • An online store:the cart and total change when products are updated.

React is particularly useful for these interfaces because it allows you to represent different visual states based on data. You can therefore build interactive applications while keeping your code organized.

Why Learn React?

  • Reusable components:write a component once and use it in several places.
  • Dynamic interfaces:update the display when the data changes.
  • Code organization:divide your application into elements that are easier to understand and maintain.
  • Large community:benefit from numerous resources, documentation, and libraries.
  • Extensive ecosystem:later explore tools such as Next.js and React Native.

These advantages explain why React is widely used in web development. Components make it easier to create increasingly complex projects, while the declarative approach simplifies the management of interface changes.

Learning React also helps you develop skills useful in many projects. You will be able to build your own applications, contribute to collaborative projects, or deepen your knowledge of front-end development.

Where Is React Used?

React can be used to create many different types of interfaces: dashboards, online stores, social networks, educational platforms, management tools, and interactive applications.

It is suitable for both personal projects and professional applications. You can start with a simple counter, a calculator, or a task list, then gradually build more complete applications.

With React Native, React principles can also be used to develop mobile applications for Android and iOS. However, the components used are different from those in the browser, because they rely on native elements.

React and Other Web Technologies

React does not replace the fundamental knowledge of web development. It builds onHTML, CSS, and JavaScript, which remain essential for understanding how an application works.

  • HTML:defines the structure of a page's content.
  • CSS:controls colors, spacing, fonts, and layout.
  • JavaScript:provides logic and interactions.
  • React:organizes interfaces into components and makes updating them easier.

For example, in React, you can write a structure that resembles HTML, apply CSS styles to it, and use JavaScript to manage events and data. These technologies therefore work together.

Prerequisites

This course assumes that you know the basics of HTML,CSS, and JavaScript: variables, functions, arrow functions, arrays, objects, destructuring, and methods such asmapandfilter. If necessary, start with the HTML, CSS, and JavaScript courses in the catalog.

What You Will Learn

Over 12 lessons, you will learn how to set up a React project, write JSX, create components, use props, manage state and events, display lists, build forms, and communicate with an API.

You will also discover theuseStateanduseEffecthooks, then learn how to create your own hooks. All these concepts will then come together in a final project: a complete task management application.

The Main Stages of the Learning Path

  • Installation:prepare the environment and create a project with Vite.
  • JSX and components:build your first reusable interfaces.
  • Props and state:pass and update component data.
  • Events and forms:manage interactions with the user.
  • Hooks and APIs:use effects and fetch data.
  • Final project:build a complete to-do list application.

Understanding Important Concepts

Before starting the next lessons, here are some terms you will encounter frequently. You do not need to master them immediately, but recognizing them will make learning easier.

  • JSX:a syntax that allows you to describe the interface directly in JavaScript.
  • Component:an independent block representing part of the interface.
  • Props:information passed from a parent component to a child component.
  • State:data that can change while the application is being used.
  • Hook:a special React function that allows components to use certain React features.

These concepts are connected to one another. A component can receive information through props, store values in its state, and display the result using JSX. You will gradually discover how they work together in an application.

How to Follow This Course

  • Practice as you learn:open your code editor and reproduce each example.
  • Learn at your own pace:make sure you understand one concept before moving on to the next.
  • Complete the exercises:they help you check your knowledge and reinforce your memory.
  • Experiment:modify the examples to observe the resulting changes.
  • Read error messages:messages displayed by development tools can help you understand problems.

Some concepts, such as state or hooks, may seem abstract at first. They become easier when you use them in practical examples. So do not hesitate to repeat the exercises several times.

A good method is to read an explanation, reproduce the code, then modify one element to observe its effect. This active practice is generally more effective than simply reading the examples.

Your First Goal

By the end of this course, you will be able to build a small React application organized into components. You will be able to display data, respond to user interactions, and manage information that changes over time.

To get started, look at the applications you use every day. Try to identify the parts that could be turned into components: buttons, menus, cards, lists, search fields, and forms.

Exercise

Open three websites or applications that you use frequently. Try to mentally divide them into components: which blocks are repeated? Which elements could be reused? Then draw the component tree of one of these interfaces on a sheet of paper.

Go Further

Imagine a small to-do list application. Identify the necessary components: a heading, a form for adding tasks, a list, and individual tasks. Then think about which data could change when the user adds or deletes a task.

Key Takeaways

  • Reactis a JavaScript library used to build user interfaces.
  • A React interface is organized intoreusable components.
  • Components can be assembled into a hierarchical tree to form an application.
  • React uses adeclarative approach: the display depends on the current data.
  • JSXallows you to describe the interface using syntax similar to HTML.
  • Propspass information between components, while stateallows you to manage changing data.
  • A basic understanding of HTML, CSS, and JavaScript is important for learning React.
  • Regular practice and building small projects make learning easier.