4. Components
Function Components vs. Legacy Class Components
In the early days of React, components were defined using JavaScript classes that extended React.Component. While these are still supported, they introduce significant cognitive load due to the this keyword, complex lifecycle methods like componentDidMount, and bloated boilerplate code. Modern React development focuses exclusively on Function Components, which are simple JavaScript functions that accept props as an argument and return UI elements. When you write a function component, React invokes your function and expects a React node back. This is more performant and easier to reason about because there is no hidden state management inside the this context to worry about. Behind the scenes, the React engine handles these functions more efficiently, as they do not require instantiation through the new keyword, reducing memory pressure on the V8 engine compared to class instances.
// Modern Function Component Example import React from 'react'; interface ButtonProps { label: string; onClick: () => void; } export const ActionButton: React.FC<ButtonProps> = ({ label, onClick }) => { return ( <button onClick={onClick} className="bg-blue-500"> {label} </button> ); };
Line-by-line explanation: import React brings the library into scope. interface ButtonProps defines the shape of our data. export const ActionButton defines our functional component. The ({ label, onClick }) syntax is object destructuring, pulling the properties out of the props object directly. The return statement provides the JSX that describes the UI branch.
| Feature | Description |
|---|---|
| Syntax | Functions are cleaner and use destructuring. |
| State | Hooks (useState) allow state in functional components. |
| Performance | Lower memory footprint without class instantiation. |
Encapsulation and Hierarchical Tree
Component composition is the secret to building scalable applications. By defining small, isolated components, you create a modular architecture. Encapsulation means that a component controls its own rendering logic and local state without exposing those details to its parent. Think of the DOM as a tree: your App component is the root, and it branches out into specialized children like Header, Sidebar, or DataCard. When you define a hierarchy, you are essentially telling React how to traverse the virtual tree during reconciliation. If a parent re-renders, React checks the children to see if they need updates, which is why isolating concerns is crucial for performance optimization.
// Parent component orchestrating children export const Dashboard = () => ( <div className="dashboard"> <Sidebar /> <main> <Header title="User Profile" /> <DataGrid /> </main>
In this structure, Dashboard knows nothing about the internal business logic of DataGrid; it only knows where to place it. This separation allows you to swap components without refactoring the entire layout, ensuring your codebase remains resilient to feature changes.
Organizing Your Project Structure
As a developer, maintaining a clean src directory is mandatory for long-term health. A common mistake is dumping all files in the root folder. You should adopt a feature-based or type-based folder structure. Start by creating a components directory inside src. Within this, group related files together. A best practice is to give every component its own folder containing an index.tsx or the component name file, along with a dedicated styles.css or test file. This ensures that if you decide to delete a component, you don't leave orphaned styles or tests behind.
src/
components/
Button/
Button.tsx
Button.test.tsx
Button.module.css
Header/
Header.tsx
This layout creates a clean mental model for the file system. When you import a component, you reference the folder path, which keeps your import statements clean and organized. By following this structure, you enable tree-shaking, allowing your build tool (like Vite) to efficiently pack only what is needed.
Knowledge Check
Ready to test your understanding of 4. Components?