Lesson 4 / 1136%

Lesson 4

Components

18 min

Create, import and compose components to build an interface.

Definition

A React component is anindependent and reusable part of a user interface, usually written as a JavaScript function that returns JSX. It is the basic building block of a React application: a complete page is built by combining several components. A component can also contain other components. This technique is called composition.

Components are at the heart of React. They allow you to divide a complex interface into smaller parts that are easier to understand and maintain. Everything you will learn next, including props, state, and events, is based on this concept.

In this lesson, you will learn how to create your own components, reuse them multiple times, organize your files, and combine several components to build a complete page. You will also discover best practices and common mistakes to avoid.

Why Use Components?

Imagine an application consisting of a navigation bar, a product list, buttons, and a footer. Without components, all the code could be grouped into a single file, which would quickly become difficult to read and modify.

With React, you can separate each part into a different component. For example, aHeadercomponent for the header, aProductcomponent for a product card, and aFootercomponent for the footer.

  • Reusability:a component can be displayed multiple times without rewriting its code.
  • Organization:each component has a specific responsibility.
  • Maintenance:changes are easier to make in well-structured code.
  • Readability:an interface divided into components is easier to understand.

A Component Is a Function

A React component is usually aJavaScript function that returns JSX. You have already encountered this structure with the mainAppcomponent. A component can be very simple, like a heading, or more elaborate, like a complete form.

Two rules are important to begin with. First, a component's name must start with an uppercase letter. Second, when a component returns multiple JSX elements, they must be grouped inside a single root element or a React fragment.

src/components/Greeting.jsx
function Greeting() {
  return <h2>Welcome to my website!</h2>;
}

export default Greeting;

Here, theGreetingfunction returns a heading. The last line,export default, allows this component to be imported into another file. This makes it easier to separate the different parts of the application.

Why Is Capitalization Important?

React distinguishes HTML elements from custom components by their names. HTML tags generally begin with a lowercase letter, while React components begin with an uppercase letter.

HTML elements and React components
// HTML element
<h1>Hello</h1>

// React component
<Greeting />

When React encounters<h1>, it recognizes an HTML element. When it encounters<Greeting />, it uses the corresponding JavaScript component. If you write its name entirely in lowercase, React will not treat it as a regular custom component.

Using a Component

To display a component created in another file, you must first import it. Then you can use it in JSX as a custom tag. You can also display multiple instances of the same component.

src/App.jsx
import Greeting from "./components/Greeting";

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

export default App;

TheGreetingcomponent appears three times on the page. However, its function was written only once. This is the principle ofcomponent reusability.

If you change the text in theGreeting.jsxfile, all three instances will display the new message. You do not need to modify each instance individually.

Browser screenshot showing the Welcome to my website message three times, with the App.jsx code beside it.

Understanding Imports and Exports

The import and export keywords allow you to share code between multiple JavaScript files. One file exports a component, while another file imports it so that it can be used.

exporting and importing a component
// In Button.jsx
function Button() {
  return <button>Click</button>;
}

export default Button;

// In App.jsx
import Button from "./components/Button.jsx";

The path./components/Button.jsxindicates where the file is located. The symbol./means that the path starts in the current directory. It is important to check the spelling of file names and import paths.

Organizing Your Files

In a React application, a common practice is to create a folder calledsrc/components. This folder contains the application's reusable components. It is often convenient to create a separate file for each important component.

component organization
src/
├── components/
│   ├── Header.jsx
│   ├── Content.jsx
│   ├── Footer.jsx
│   └── Greeting.jsx
├── App.jsx
├── main.jsx
└── index.css

This structure makes it easy to find the different parts of your project. If you want to modify the navigation bar, you can open the corresponding file directly. This avoids having to search through a single file containing the entire application.

To keep a project readable, use component names that clearly describe their purpose. For example,ProductCardis more descriptive than a generic name likeElement1. This habit becomes especially useful as your project grows.

Composing Components

A component can contain several other components. This technique is called composition. It allows you to build a complete interface from smaller parts. Together, these parts form a component tree.

composition: Header, Content, Footer
function Header() {
  return (
    <header>
      <h1>My Blog</h1>
    </header>
  );
}

function Content() {
  return <p>Here is the page content.</p>;
}

function Footer() {
  return <footer>© 2026 - My Blog</footer>;
}

function App() {
  return (
    <div>
      <Header />
      <Content />
      <Footer />
    </div>
  );
}

export default App;

In this example, theAppcomponent contains three components:Header,Content, andFooter. It represents the parent component, while the other three are its child components.

This organization resembles a family tree. A child component can itself contain other components. For example, a header can contain a logo, a navigation menu, and a login button.

Component tree diagram with App at the top and three child components: Header, Content, and Footer.

Understanding Parent-Child Relationships

In React, a parent component is a component that displays another component. The displayed component is called a child component. This relationship is important for organizing the interface and passing information between components.

example of a parent-child relationship
function Logo() {
  return <strong>My Website</strong>;
}

function Menu() {
  return <nav>Home | Contact</nav>;
}

function Header() {
  return (
    <header>
      <Logo />
      <Menu />
    </header>
  );
}

Here, theHeadercomponent is the parent ofLogoandMenu. This structure makes it easier to separate responsibilities. Each component has a clearly defined purpose.

Returning Multiple Elements with a Fragment

A React component must return a single root element. If you want to display multiple elements without adding an extra HTML tag, you can use a React fragment.

using a React fragment
function Introduction() {
  return (
    <>
      <h2>Welcome!</h2>
      <p>I'm learning about React components.</p>
    </>
  );
}

export default Introduction;

The symbols<>and</>represent a fragment. They group JSX elements without creating an additional HTML element on the page. This is useful for keeping the HTML structure simple.

Creating a Reusable Component

To understand the benefits of reusability, imagine a component that represents an information card. You can use the same component in several places throughout your application.

example of a reusable card
function Card() {
  return (
    <article>
      <h3>Important Information</h3>
      <p>Here is a short description.</p>
    </article>
  );
}

function App() {
  return (
    <main>
      <Card />
      <Card />
      <Card />
    </main>
  );
}

export default App;

The three cards currently display the same content. In the next lesson, you will discover props, which allow you to pass different information to each instance. This will enable you to display multiple products using a single component.

When Should You Create a New Component?

You do not need to turn every HTML element into a React component. The goal is to divide the interface logically. Creating a new component becomes useful when a part of the interface has a specific purpose or needs to be reused.

  • Repeated element:a product card displayed multiple times.
  • Independent section:a header, footer, or navigation bar.
  • Code that is too long:a complex section that deserves to be isolated.
  • Specific responsibility:a form, list, or custom button.

A good component should be simple enough to understand quickly. If a function contains several independent parts, you can consider separating them. On the other hand, avoid creating too many small components without a real purpose.

Good to Know

Avoid defining a component inside another component. When a parent component runs again, a function defined inside it is recreated. React may then consider the child component to be a new type, which can cause it to lose its state. Instead, declare components at the top level of the file.

Common Mistakes

  • Forgetting capitalization:a component namedgreetingwill not be interpreted as a regular React component.
  • Forgetting the import:using a component without importing it can cause an error.
  • Forgetting the export:without the appropriate export, a component cannot be imported from another file.
  • Using the wrong path:check the file name and its location.
  • Forgetting to close a tag:JSX elements must be properly closed.
  • Nesting component definitions:avoid declaring a component inside another component.

Tips for Organizing Your Application

As your application grows, maintaining a clear structure becomes increasingly important. Start by identifying the main sections of your interface, then gradually turn them into components.

  • Give your components descriptive names.
  • Group reusable components in a dedicated folder.
  • Avoid unnecessarily long components.
  • Reuse components when several elements share the same structure.
  • Regularly check that each component works correctly.

These best practices are not mandatory for a very small application, but they become extremely useful in larger projects. They make collaboration, maintenance, and adding new features much easier.

Exercise

Create three components in separate files:Header,Content, andFooter. Place them in thesrc/componentsfolder, then import them into theAppcomponent. Check that all three sections display correctly in the browser.

Go Further

Add aMenucomponent insideHeader. This menu should contain three links: Home, About, and Contact. Then create aCardcomponent and display it three times in the Content section. Observe the component tree you have just built.

Key Takeaways

  • A React component is usually a JavaScript function that returns JSX.
  • A component's name must begin with an uppercase letter.
  • Components allow you to divide an interface into independent and reusable parts.
  • The import and exportkeywords allow you to share components between files.
  • Thesrc/componentsfolder is commonly used to organize reusable components.
  • A component can contain other components: this is called composition.
  • Parent-child relationships form a component tree.
  • React fragments allow you to group multiple JSX elements without adding an HTML tag.
  • It is better to declare components at the top level of your files.
  • Good organization makes reusability, maintenance, and future development easier.