Components and Props: Building Reusable UI
In the world of modern web development, especially with React and Next.js, the concept of Components is the most fundamental building block. If you understand how to structure components and pass data using Props, you have mastered the core philosophy of React. This lesson will guide you through creating modular, maintainable, and reusable user interfaces.
What are Components?
Think of a component as a self-contained "LEGO" block. Each block represents a part of the user interface, such as a button, a navigation bar, or a profile card. By combining these small blocks, you build complex applications. In React, a component is essentially a JavaScript function that returns HTML-like code (JSX).
The Component Hierarchy
React applications are structured as a tree of components. Here is a simple visualization of how a dashboard might be broken down:
[App Component]
|-- [Navbar Component]
|-- [Sidebar Component]
|-- [MainContent Component]
|-- [StatCard Component]
|-- [StatCard Component]
|-- [Chart Component]
Creating Your First Component
In modern React development, we primarily use Functional Components. They are simple JavaScript functions that accept "props" as an argument and return JSX.
function WelcomeMessage() {
return (
<div>
<h2>Hello, Welcome to our App!</h2>
<p>We are glad to have you here.</p>
</div>
);
}
Understanding Props (Properties)
Components would be quite boring if they always displayed the same data. Props (short for properties) allow us to pass data from a parent component to a child component. This makes components dynamic and reusable.
Important Rule: Props are read-only. A component must never modify its own props. This ensures a predictable "one-way data flow."
Example: Reusable User Card
Instead of writing the same HTML for every user, we create one UserCard component and pass different data to it.
// The Child Component
function UserCard(props) {
return (
<div className="card">
<h3>Name: {props.name}</h3>
<p>Role: {props.role}</p>
</div>
);
}
// The Parent Component
function App() {
return (
<div>
<h1>Team Members</h1>
<UserCard name="Alice" role="Developer" />
<UserCard name="Bob" role="Designer" />
</div>
);
}
Destructuring Props
To make the code cleaner, developers often use JavaScript destructuring to extract values directly from the props object.
function UserCard({ name, role }) {
return (
<div>
<h3>{name}</h3>
<p>{role}</p>
</div>
);
}
Real-World Use Cases
- E-commerce Product Listing: A single
ProductItemcomponent used to display hundreds of different products by passing different props (image, price, title). - Form Inputs: A reusable
Inputcomponent that handles styling and validation logic, used across Login, Signup, and Profile pages. - Navigation Menus: Passing an array of links as props to a
Menucomponent to generate navigation dynamically.
Common Mistakes to Avoid
- Modifying Props: Never try to change
props.name = "New Name". If data needs to change, use State (which we will cover in the next lesson). - Not Returning JSX: Always ensure your component function has a
returnstatement. If you miss it, React will throw an error. - Naming Conventions: Component names must start with a capital letter (e.g.,
MyComponent, notmyComponent). React uses this to distinguish between HTML tags and custom components. - Prop Drilling: Passing props through five levels of components just to reach a deeply nested child. This can be solved later using Context API or State Management.
Interview Notes: Key Talking Points
- What is the difference between a Component and an Element? An element is what you see on the screen (a plain object), while a component is a function or class that produces those elements.
- Are props immutable? Yes, props are immutable from the perspective of the child component. They can only be changed by the parent component that owns them.
- What is "Composition"? It is the practice of combining small, simple components to build larger, complex ones, rather than using inheritance.
- What are "Children" props? React provides a special prop called
props.childrenwhich allows you to pass nested JSX elements into a component.
Summary
Components and Props are the foundation of any React or Next.js application. By breaking your UI into small, functional pieces, you make your code easier to test, debug, and scale. Remember that data flows down from parents to children via props, and keeping components "pure" (not modifying their input) is key to a healthy codebase.
In our next topic, State Management, we will learn how to make these components interactive by allowing them to "remember" and update data over time.
Related Topics: Introduction to JSX | State and Lifecycle | Handling Events