04-components.html
html10 kB233 linesIn Tutorial.zip
Tutorial.zip04-components.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React Components - React Tutorial</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<header>
<h1>⚛️ React Components</h1>
<p>Chapter 4 of 15</p>
</header>
<div class="content">
<h2>What are Components?</h2>
<p>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.</p>
<div class="info-box">
<strong>💡 Think of Components as Custom HTML Elements:</strong> Just like you use <code class="inline-code"><div></code> or <code class="inline-code"><button></code>, you can create your own elements like <code class="inline-code"><Header></code> or <code class="inline-code"><UserProfile></code>!
</div>
<h2>Types of Components</h2>
<p>There are two types of components in React:</p>
<ul>
<li><strong>Function components</strong> (Recommended)</li>
<li><strong>Class components</strong> (Legacy, but still supported)</li>
</ul>
<h2>Function Components</h2>
<p>A Function component is a JavaScript function that returns HTML (JSX). This is the modern way to write React components:</p>
<pre><code><span class="keyword">function</span> <span class="function">Car</span>() {
<span class="keyword">return</span> <h2>Hi, I am a Car!</h2>;
}</code></pre>
<p>You can also use arrow function syntax:</p>
<pre><code><span class="keyword">const</span> <span class="function">Car</span> = () => {
<span class="keyword">return</span> <h2>Hi, I am a Car!</h2>;
}</code></pre>
<div class="success-box">
<strong>✨ Best Practice:</strong> Component names must start with a capital letter! This helps React distinguish between regular HTML tags and components.
</div>
<h2>Rendering a Component</h2>
<p>To use a component in your application, use it like an HTML tag:</p>
<pre><code><span class="keyword">const</span> root = ReactDOM.<span class="function">createRoot</span>(document.<span class="function">getElementById</span>(<span class="string">'root'</span>));
root.<span class="function">render</span>(<Car />);</code></pre>
<h2>Class Components</h2>
<p>A class component must include the <code class="inline-code">extends React.Component</code> statement and a <code class="inline-code">render()</code> method:</p>
<pre><code><span class="keyword">class</span> Car <span class="keyword">extends</span> React.Component {
<span class="function">render</span>() {
<span class="keyword">return</span> <h2>Hi, I am a Car!</h2>;
}
}</code></pre>
<div class="info-box">
<strong>📚 History Note:</strong> Before React Hooks were introduced in 2019, class components were the only way to use state and lifecycle methods. Now, function components with Hooks are the recommended approach.
</div>
<h2>Components in Components</h2>
<p>We can refer to components inside other components. This is called composition:</p>
<pre><code><span class="keyword">function</span> <span class="function">Car</span>() {
<span class="keyword">return</span> <h2>I am a Car!</h2>;
}
<span class="keyword">function</span> <span class="function">Garage</span>() {
<span class="keyword">return</span> (
<>
<h1>Who lives in my Garage?</h1>
<Car />
</>
);
}
<span class="keyword">const</span> root = ReactDOM.<span class="function">createRoot</span>(document.<span class="function">getElementById</span>(<span class="string">'root'</span>));
root.<span class="function">render</span>(<Garage />);</code></pre>
<h2>Multiple Components</h2>
<p>You can use the same component multiple times:</p>
<pre><code><span class="keyword">function</span> <span class="function">Car</span>() {
<span class="keyword">return</span> <h2>I am a Car!</h2>;
}
<span class="keyword">function</span> <span class="function">Garage</span>() {
<span class="keyword">return</span> (
<>
<h1>My Garage</h1>
<Car />
<Car />
<Car />
</>
);
}</code></pre>
<h2>Components in Files</h2>
<p>React is all about re-using code. It's recommended to split your components into separate files for better organization:</p>
<h3>Car.js</h3>
<pre><code><span class="keyword">function</span> <span class="function">Car</span>() {
<span class="keyword">return</span> <h2>Hi, I am a Car!</h2>;
}
<span class="keyword">export default</span> Car;</code></pre>
<h3>App.jsx</h3>
<pre><code><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>
<span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom/client'</span>
<span class="keyword">import</span> Car <span class="keyword">from</span> <span class="string">'./Car.jsx'</span>
ReactDOM.<span class="function">createRoot</span>(document.<span class="function">getElementById</span>(<span class="string">'root'</span>)).<span class="function">render</span>(
<React.StrictMode>
<Car />
</React.StrictMode>
)</code></pre>
<div class="success-box">
<strong>✨ File Organization:</strong> Create a separate file for each component. Use <code>.jsx</code> extension for files containing JSX. This makes your code easier to maintain and test!
</div>
<h2>Component Folder Structure (Vite)</h2>
<p>A typical Vite React project structure might look like:</p>
<pre><code>src/
├── components/
│ ├── Header.jsx
│ ├── Footer.jsx
│ ├── Car.jsx
│ └── Garage.jsx
├── App.jsx
└── main.jsx</code></pre>
<h2>Named Exports vs Default Exports</h2>
<h3>Default Export (One per file)</h3>
<pre><code><span class="comment">// Car.jsx</span>
<span class="keyword">function</span> <span class="function">Car</span>() {
<span class="keyword">return</span> <h2>Car</h2>
}
<span class="keyword">export default</span> Car
<span class="comment">// Importing in Vite</span>
<span class="keyword">import</span> Car <span class="keyword">from</span> <span class="string">'./Car.jsx'</span></code></pre>
<h3>Named Export (Multiple per file)</h3>
<pre><code><span class="comment">// Vehicles.jsx</span>
<span class="keyword">export function</span> <span class="function">Car</span>() {
<span class="keyword">return</span> <h2>Car</h2>
}
<span class="keyword">export function</span> <span class="function">Truck</span>() {
<span class="keyword">return</span> <h2>Truck</h2>;
}
<span class="comment">// Importing</span>
<span class="keyword">import</span> { Car, Truck } <span class="keyword">from</span> <span class="string">'./Vehicles'</span>;</code></pre>
<h2>Component Best Practices</h2>
<ul>
<li><strong>Single Responsibility:</strong> Each component should do one thing well</li>
<li><strong>Reusable:</strong> Design components to be used in multiple places</li>
<li><strong>Small:</strong> Keep components small and manageable (ideally under 200 lines)</li>
<li><strong>Named Well:</strong> Use descriptive names that explain what the component does</li>
<li><strong>Organized:</strong> Put each component in its own file</li>
</ul>
<h2>Real-World Example</h2>
<p>Here's a more realistic example with multiple components:</p>
<pre><code><span class="keyword">function</span> <span class="function">Header</span>() {
<span class="keyword">return</span> (
<header>
<h1>My Website</h1>
<nav>
<a href=<span class="string">"/"</span>>Home</a>
<a href=<span class="string">"/about"</span>>About</a>
</nav>
</header>
);
}
<span class="keyword">function</span> <span class="function">Content</span>() {
<span class="keyword">return</span> (
<main>
<h2>Welcome!</h2>
<p>This is my React application.</p>
</main>
);
}
<span class="keyword">function</span> <span class="function">Footer</span>() {
<span class="keyword">return</span> (
<footer>
<p>© <span class="number">2025</span> My Website</p>
</footer>
);
}
<span class="keyword">function</span> <span class="function">App</span>() {
<span class="keyword">return</span> (
<>
<Header />
<Content />
<Footer />
</>
);
}</code></pre>
<div class="warning-box">
<strong>⚠️ Remember:</strong> Always capitalize component names! React treats components starting with lowercase letters as DOM tags.
</div>
<h2>What's Next?</h2>
<p>Now that you understand components, you're ready to learn about Props - how to pass data between components!</p>
</div>
<div class="navigation">
<a href="03-jsx.html" class="nav-button">← Previous: JSX</a>
<a href="index.html" class="home-button">🏠 Home</a>
<a href="05-props.html" class="nav-button">Next: Props →</a>
</div>
</div>
</body>
</html>
In Tutorial.zip
01-introduction.html6 kB02-getting-started.html9 kB03-jsx.html10 kB04-components.html10 kB05-props.html11 kB06-state.html14 kB07-hooks.html15 kB08-events.html15 kB09-forms.html17 kB10-router.html9 kB11-styling.html11 kB12-useeffect.html11 kB13-usecontext.html13 kB14-useref.html13 kB15-custom-hooks.html19 kBindex.html5 kBstyles.css4 kB