Day-10 Props and Component Composition in React
Day-10 Props and Component Composition in React
Props (short for properties) are the mechanism in React that allows data to flow from a parent
component to a child component. They provide a way for components to accept inputs that can
customize their behavior or appearance.
Props are immutable, meaning that a component cannot modify the props it receives. They are read-
only, and their main purpose is to pass data, event handlers, or other functions from a parent
component down to a child component.
Read-Only: Props are immutable, and their values cannot be changed by the child
component that receives them.
Passed from Parent to Child: The parent component passes props to its child components.
Customizable: Props can contain different types of data, including strings, numbers, objects,
arrays, and functions.
function Greeting(props) {
function App() {
In this example:
The parent component (App) passes a value for the name prop, and the Greeting component
uses that value to render a personalized greeting message: Hello, John!.
function UserProfile(props) {
return (
<div>
<h2>{props.name}</h2>
</div>
);
function App() {
return (
<div>
</div>
);
In this example:
The parent component (App) passes these props with different values, so each user profile
will display different information.
Props can also be used to pass functions, which can be invoked inside the child component.
function Button(props) {
function App() {
alert("Button clicked!");
};
In this example:
When the button is clicked, the handleClick function is called, displaying an alert.
2. Component Composition
Component Composition refers to the practice of combining multiple smaller components into a
larger, more complex component. This helps you break down a user interface into reusable, modular
pieces, which leads to better code organization and easier maintenance.
React’s component-based architecture makes it easy to build large UIs by composing smaller
components.
Let's say you want to create a reusable Card component that displays a title and some content. By
using props, you can customize the content of each card when using it.
return (
<div className="card">
<h2>{title}</h2>
<p>{content}</p>
</div>
);
function App() {
return (
<div>
<Card title="First Card" content="This is the content of the first card." />
<Card title="Second Card" content="This is the content of the second card." />
<Card title="Third Card" content="This is the content of the third card." />
</div>
);
In this example:
You can reuse the Card component multiple times, passing different title and content values
each time to customize its output.
You can also nest components within each other to build more complex UIs.
function Header() {
function Footer() {
function MainContent() {
return (
<main>
<h2>Welcome to My Website</h2>
</main>
);
}
function App() {
return (
<div>
<Header />
<MainContent />
<Footer />
</div>
);
In this example:
The Header, MainContent, and Footer components are all separate, reusable components.
The App component is composed of these smaller components. This structure allows you to
create a page layout by combining them, which is the essence of component composition.
In addition to passing data, you can also pass entire components as props. This is useful when you
want a component to decide what content to display.
return (
<div className="card">
<h2>{title}</h2>
<div>{children}</div>
</div>
);
function App() {
return (
<div>
</div>
);
In this example:
The children prop allows you to pass arbitrary content into the Card component, which
makes it more flexible and reusable.
By combining props and component composition, you can create complex and customizable UIs.
Here’s an example where both concepts are used together:
return (
<div className="card">
<h2>{title}</h2>
<p>{content}</p>
</div>
);
function App() {
alert('Button clicked!');
};
return (
<div>
<Card
title="Card 1"
onClickButton={handleClick}
/>
<Card
title="Card 2"
onClickButton={handleClick}
/>
</div>
);
In this example:
The Card component accepts a title, content, and a button click handler (onClickButton) as
props.
The Button component is passed as a child of the Card and is customized via the props
passed to it.
Conclusion
Props: The essential concept for passing data between parent and child components in
React. Props are immutable and allow customization of a component’s behavior or content.
By combining props and composition, you can build highly flexible and reusable components
that form the foundation of React applications.