How to Pass a React Component into Another to Transclude Content?
Last Updated :
08 Oct, 2024
Transclusion refers to Passing a react component's content to another component. Passing component to another in react enable use to render the data inside other component. This content can be rendered as component's children.
Approach
To pass a react component to another in react we will pass it as the children of that component. It will be accessible using props.children and we can render it as required. We can also pass the component as a prop to render its content.
Creating React Application
Before proceeding to the approach you have to create a React app.
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder, i.e., foldername, move to it using the following command:
cd foldername
Project Structure:
It will look like the following.

Approch 1: Using prop.children
To transclude the content we can enclose a component inside other component as given below
<First >
<Second></Second>
</First>
The content will be accessible with the help of props and will be rendered as children
in this way the component First can access the Second component using this.props.children attribute.
{props.children}
Example: This example demonstrate content transclusion by rendering the component as a child by enclosing it with other component.
JavaScript
// App.js
import React from "react";
const App = () => {
return (
<div className="App">
<h1>App</h1>
<First>
<Second />
</First>
</div>
);
};
const First = (props) => {
return (
<div>
<h2>First Component</h2>
{props.children}
</div>
);
};
const Second = () => {
return (
<div>
<h3>Second Component</h3>
</div>
);
};
export default App;
Approach 2: Passing Componnet as Prop
We can pass the react component as prop into another component to transclude the content.
<First secondcomp= {<Second/>} >
so the First component can access the component using props.secondcomp attribute.
{props.second}
Example: This example passes a react component as prop to render the content into another component.
JavaScript
// Filename
import React from "react";
const App = () => {
return (
<div className="App">
<h1>App</h1>
<First secondcomp={<Second />} />
</div>
);
};
const First = (props) => {
return (
<div>
<h2>First Component</h2>
{props.secondcomp}
</div>
);
};
const Second = () => {
return (
<div>
<h3>Second Component</h3>
</div>
);
};
export default App;
Output: Both the approach will give the same output
Similar Reads
How to Pass Data from One Component to Another Component in ReactJS? In ReactJS, components are the building blocks of your user interface. Components allow you to create modular, reusable UI elements, but sometimes these components need to communicate with each other.In this article, we will explore the different methods to pass data between components in ReactJS.1.
5 min read
How to Link a Custom React Component <MyButton> to Another Page ? React is a powerful JavaScript library for building user interfaces, and one of its key features is the ability to create reusable components. In this article, we will walk you through the process of linking a custom React component, <MyButton>, to another page using react-router-dom.Prerequis
4 min read
How to Pass JSON Values into React Components ? In React, you can pass JSON values into components using props. Props allow us to send data from a parent component to its child components. When rendering a component, you can pass the JSON values as props. These props can then be accessed within the componentâs function or class, allowing you to d
3 min read
How to Pass Value from One Child Component to Another in VueJS ? Vue.js is a JavaScript framework used in building powerful and beautiful user interfaces. The key feature of Vue.js is its component-based architecture that allows the developers to create reusable and modular components. In this article, we will learn how to pass value from one child component to a
3 min read
How to add Event Listeners to wrapped Component in ReactJS ? React provides a powerful way of DOM manipulation by introducing the concept of virtual DOM. It is mainly used to build single-page applications. There are times when you need to add event listeners. This can be achieved by using the concept of forwarding refs and leveraging React's lifecycle method
2 min read