What are Components?
Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML via a render function.
<div> or <button>, you can create your own elements like <Header> or <UserProfile>!
Types of Components
There are two types of components in React:
- Function components (Recommended)
- Class components (Legacy, but still supported)
Function Components
A Function component is a JavaScript function that returns HTML (JSX). This is the modern way to write React components:
function Car() {
return <h2>Hi, I am a Car!</h2>;
}
You can also use arrow function syntax:
const Car = () => {
return <h2>Hi, I am a Car!</h2>;
}
Rendering a Component
To use a component in your application, use it like an HTML tag:
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Car />);
Class Components
A class component must include the extends React.Component statement and a render() method:
class Car extends React.Component {
render() {
return <h2>Hi, I am a Car!</h2>;
}
}
Components in Components
We can refer to components inside other components. This is called composition:
function Car() {
return <h2>I am a Car!</h2>;
}
function Garage() {
return (
<>
<h1>Who lives in my Garage?</h1>
<Car />
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
Multiple Components
You can use the same component multiple times:
function Car() {
return <h2>I am a Car!</h2>;
}
function Garage() {
return (
<>
<h1>My Garage</h1>
<Car />
<Car />
<Car />
</>
);
}
Components in Files
React is all about re-using code. It's recommended to split your components into separate files for better organization:
Car.js
function Car() {
return <h2>Hi, I am a Car!</h2>;
}
export default Car;
App.jsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import Car from './Car.jsx'
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<Car />
</React.StrictMode>
)
.jsx extension for files containing JSX. This makes your code easier to maintain and test!
Component Folder Structure (Vite)
A typical Vite React project structure might look like:
src/
├── components/
│ ├── Header.jsx
│ ├── Footer.jsx
│ ├── Car.jsx
│ └── Garage.jsx
├── App.jsx
└── main.jsx
Named Exports vs Default Exports
Default Export (One per file)
// Car.jsx
function Car() {
return <h2>Car</h2>
}
export default Car
// Importing in Vite
import Car from './Car.jsx'
Named Export (Multiple per file)
// Vehicles.jsx
export function Car() {
return <h2>Car</h2>
}
export function Truck() {
return <h2>Truck</h2>;
}
// Importing
import { Car, Truck } from './Vehicles';
Component Best Practices
- Single Responsibility: Each component should do one thing well
- Reusable: Design components to be used in multiple places
- Small: Keep components small and manageable (ideally under 200 lines)
- Named Well: Use descriptive names that explain what the component does
- Organized: Put each component in its own file
Real-World Example
Here's a more realistic example with multiple components:
function Header() {
return (
<header>
<h1>My Website</h1>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
);
}
function Content() {
return (
<main>
<h2>Welcome!</h2>
<p>This is my React application.</p>
</main>
);
}
function Footer() {
return (
<footer>
<p>© 2025 My Website</p>
</footer>
);
}
function App() {
return (
<>
<Header />
<Content />
<Footer />
</>
);
}
What's Next?
Now that you understand components, you're ready to learn about Props - how to pass data between components!