Components are the building blocks of a React application. They allow you to split the UI into independent, reusable pieces that can be managed and updated separately.
Using components helps to:
.jsx
extension.Functional components are simple JavaScript functions that return JSX. They are the most common type of component in React.
Let's create a Header.jsx
component:
import React from "react";
const Header = () => {
return (
<>
<h1>React App</h1>
</>
);
};
export default Header;
Next, import the Header.jsx
component in App.jsx
:
import Header from "./Header";
Now, use the Header.jsx
component inside the App.jsx
component:
return (
<>
<Header />
<p>This is my first React App.</p>
</>
);
By breaking down your UI into smaller components, you can manage and reuse code more effectively, leading to a more organized and scalable application.
Objective: Create a new functional component called Footer.jsx
that renders a footer message. Import and use this Footer
component in App.jsx
below the paragraph.
Create Footer.jsx
:
import React from "react";
const Footer = () => {
return (
<>
<footer>© 2024 React.js Masters Course Team</footer>
</>
);
};
export default Footer;
Import Footer
in App.jsx
:
import Footer from "./Footer";
Use the Footer
component in App.jsx
:
return (
<>
<Header />
<p>This is my first React App.</p>
<Footer />
</>
);
This task will help you practice creating, importing, and using components in a React application.