Lesson 4
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.
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.
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.
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.
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 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.
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.
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.

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.
// 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.
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.
src/
├── components/
│ ├── Header.jsx
│ ├── Content.jsx
│ ├── Footer.jsx
│ └── Greeting.jsx
├── App.jsx
├── main.jsx
└── index.cssThis 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.
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.
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.

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.
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.
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.
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.
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.
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.
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.
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.
greetingwill not be interpreted as a regular React component.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.
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
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
Menucomponent 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.src/componentsfolder is commonly used to organize reusable components.