Posted on 5/16/2025 9:09:39 AM by Admin

Component Composition and Reusability in React

Since you are already familiar with the concept of components and now must understand how to create and use them in your React projects, it's time to explore their power in more detail. The component reusability is one of the core React features that make it useful in creating user-friendly web applications without duplicating the code. The Component Reusability implies creating the components in a way that they are self-contained and can be used or customized in different parts of your application without duplicating the logic. This not only enhances the code maintainability but also ensures consistency in user interfaces.

The component reusability is highly dependent on props, which we use to transfer data and tailor components according to our needs. In this article, we will explore the component composition techniques and props.children to create reusable and flexible UI blocks.

Component Reusability

Suppose you are working on a complex website with multiple containers with slightly different content. Without reusability, you'll have to repeatedly redefine each container with the same styling code, leading to redundant and duplicate code. If you want to change the borders or padding of the container, you will have to do that individually for each container, wasting lots of time and resources with the increased risk of inconsistencies in the UI.

In React, the component reusability resolves the abovementioned problem by allowing us to create generic components that can be customized with props. This allows us to build our UI from a pre-defined set of well-structured and reusable elements. The props allow us to configure the components, allowing parent components to pass data to the child components and modify their appearance.

Unlocking Content Flexibility: Understanding props.children

props.children are an implicit prop that contains the React or JSX elements nested within a component. The React components render the predefined elements and can contain and render dynamic content defined between their opening and closing tags. To understand this, let's create a simple component named Container and add the following code to it:


function Container(props) {
  return
(
    <div style={{ border: '2px solid red', margin: '20px', backgroundColor: 'black', color: 'white'}}>
      {props.children}
    </div>
  );

}


export default Container;

In the above code, I have defined a simple component that does not know what content will be placed inside it. The content is determined by the props.children passed by the App.js. The following code goes under App.js:


import logo from './logo.svg';
import './App.css';
import Container from './Container';

function App() {
  return
(
    <div>
      <Container>
        <p>This is Container Component.</p>
      </Container>
      <Container>
        <h2>Another Container</h2>
        <button>Click Me</button>
      </Container>
    </div>
  );

}

export default App;

Here, we have called the container component to define 2 containers with different content but the same formatting. The <p>, <h2>, and <button> tags in the above code are passed to the Container component as children.

Here's what our components look like:

Reusable Component
Multiple Cards with Single Component

Building Structured UIs: Composition with props.children For Layouts

The props.children pattern is specifically useful for creating reusable layout components for common UI structures like sidebars and cards. Let's create a Card component that accepts any content as its children:


function Card(props) {
  return
(
    <div style={{ border: '2px solid red',
borderRadius: '5px', margin: '10px', backgroundColor: 'black', color:'white'
}}>
      <h2>{props.name}</h2>
      <p>{props.description}</p>
      <div>{props.children}</div>
    </div>
  );

}

export default Card;

In the above code, I have defined a Card component that takes name and description as arguments and props.children accept any element passed to the component via the App.js. In App.js, add the following code:


import logo from './logo.svg';
import './App.css';
import Card from './Card';

function App() {
  return
(
  <div>
  <div>
    <Card name="New
Card"
description="This
is the first Card.">
      <p>This is the cards content</p>
      <button>Click Me</button>
    </Card>
     </div>

     <div>
     <Card name="Another
Card"
description="This
is the another Card.">
      <p>This is the second card content</p>
      <button>Click Me</button>
    </Card>
     </div>
     </div>
  );

}

export default App;

In App.js, we have created 2 cards using the Card component. The Card component expects the name and description to be provided when called, but does not know about the <p> and <button> in advance. They are handled by props.children pattern.

Here's how our components look like when rendered:

Dynamic Content using props.children
Dynamic Content using props.children

Similarly, we can create the reusable layouts for sidebars and other reusable parts of our webpages.

Enhancing Flexibility: Passing Props Alongside props.children

The power of composition is highly visible when we combine the explicit props with props.children. The explicit props allow us to modify the particular aspects of the component according to our needs and props.children allow us to define the arbitrary content.

Suppose we have a Panel component having an explicit prop and the props.children:


function Panel(props) {
  return
(
    <div style={{ border: '2px solid black',
margin: '10px', color: 'red', backgroundColor:'white'}}>
      <h2>{props.name}</h2>
      <div>{props.children}</div>
    </div>
  );

}

export default Panel

In the above code, the props.name is the explicit prop, and the props.children accept the content passed dynamically through the App.js.


import logo from './logo.svg';
import './App.css';
import Panel from './Panel';

function App() {
  return
(
    <Panel name="First Panel">
      <p>This is the content inside the panel handled by
props.children</p>
    </Panel>
  );

}

export default App;

Here's how our panel looks when rendered:

Multiple Panels with Different Content
Multiple Panels with Different Content

Using the Panel component, we can create as many panels with custom content as needed.

Best Practices for Reusable Components

To make the most of the reusable components, use the following best practices:

  • Try to make the components as generic and adaptable as possible, focusing on their core functionality and structure to make them highly reusable.
  • Control the appearance, behavior, and data of your components by extensively using props to enhance their adaptability.
  • Use props.children to enhance content flexibility and layout composition.

Reusable components are a powerful tool for creating highly dynamic and user-friendly interfaces without redefining the code blocks. The props.children provide an efficient way of dynamically changing a component's data with a predefined structure and functionality.


Sharpen Your Skills with These Next Guides