A component in React is a reusable, self-contained piece of UI. You can think of components as small building blocks that you combine to build a full web page or entire app.
Components make your code modular, reusable, and easier to maintain.
? Small UI block ♻️ Reusable ?️ Easy to maintain
Navbar).Modern React mainly uses functional components + hooks, so we will focus on those.
A basic React functional component:
General form:
// Basic functional component pattern
function ComponentName() {
return (
<div>
<h2>Some UI here</h2>
</div>
);
}
export default ComponentName;
Here is a simple functional component called Welcome:
// Functional component that shows a welcome message
function Welcome() {
return (
<div>
<h2>Hello from a Component!</h2>
<p>This is a functional component.</p>
</div>
);
}
export default Welcome;
This component can now be imported and used inside another component like <Welcome />.
To display the Welcome component on the page, import and render it inside App.js (or another component):
// App component that renders the Welcome component
import Welcome from "./Welcome";
function App() {
return (
<div>
<h1>My React App</h1>
<Welcome />
</div>
);
}
export default App;
Notice that we use the component as a custom tag: <Welcome />.
Navbar, UserCard).Navbar.jsx).export default and import it where needed.
// Header.jsx - component file
function Header() {
return <h2>Site Header</h2>;
}
export default Header;
// App.js - importing and using Header
import Header from "./Header";
function App() {
return (
<div>
<Header />
<p>Welcome to my site!</p>
</div>
);
}
export default App;
A component can contain other components — this is called composition. It lets you build big UIs from small, focused pieces.
// Card.jsx - composing smaller components
function Title() {
return <h3>Card Title</h3>;
}
function Description() {
return <p>This is a simple card description.</p>;
}
function Card() {
return (
<div className="card">
<Title />
<Description />
</div>
);
}
export default Card;
Here, Card is made up of two smaller components: Title and Description.
If you render the App component that uses Welcome, you will see:
Welcome component: Hello from a Component!.When you change the text inside Welcome, only that part of the UI updates, showing how components isolate pieces of the interface.
Header.jsx, MainContent.jsx).components/ folder for better project structure.Header, Content, and Footer.App.js to form a simple page layout.Goal: Understand what React components are, how to define them, and how to compose multiple components together to build a complete UI.