React JS Component Composition and Nested Components
Last Updated :
18 Mar, 2024
React JS Component Composition allows you to build complex UIs by combining smaller, reusable components. Nested Components refer to components that are used within other components, enabling a modular and structured approach to building React applications.
Steps to setup React Application
Step 1: Create a new react app and enter it by running the following commands
npx create-react-app composition-vs-nesting
Step 2: Change the directory
cd composition-vs-nesting
Project Structure:
PROJECT STRUCTURE FOR COMPOSITION AND NESTING OF COMPONENTSUpdated Dependencies: The updated dependencies in package.json file should look like this.
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Component Composition
Component Composition means combining various other smaller React components in a bigger component so as to create modular architecture in the web application. It breaks the application into smaller reusable components and hence makes the user interface look much more simple.
Example: Illustration to showcase the use of composition component.
JavaScript
// Layout.js
import React from 'react'
import Header from './Header'
import SideBar from './SideBar'
import Footer from './Footer'
const Layout = () => {
return (
<div>
<b>Layout ---</b>
<Header />
<SideBar />
<Footer />
</div>
)
}
export default Layout
JavaScript
// Header.js
import React from 'react'
const Header = () => {
return (
<div>Header</div>
)
}
export default Header
JavaScript
// SideBar.js
import React from 'react'
const SideBar = () => {
return (
<div>SideBar</div>
)
}
export default SideBar
JavaScript
// Footer.js
import React from 'react'
const Footer = () => {
return (
<div>Footer</div>
)
}
export default Footer
JavaScript
// App.js
import Layout from "./components/compositionOfComponents/Layout";
import Parent from "./components/nestingComponents/Parent";
function App() {
return (
<div>
<h3>Composition of Components</h3>
<Layout />
</div>
);
}
export default App;
Output :
OUTPUT IMAGE FOR COMPOSITION OF COMPONENTSNested Component
Nesting of Components creates the parent-child relationship between the nested components. In this approach, children components are nested by including them in the JSX markup of parent component. It helps in organizing the components in proper structure.
Example: Illustration to showcase the use of nested component.
JavaScript
// Parent.js
import React from 'react'
import Child from './Child'
const Parent = () => {
return (
<div>
<b>This is Parent</b>
<Child />
</div>
)
}
export default Parent
JavaScript
// Child.js
import React from 'react'
const Child = () => {
return (
<div>Child nested within parent</div>
)
}
export default Child
JavaScript
// App.js
import Layout from "./components/compositionOfComponents/Layout";
import Parent from "./components/nestingComponents/Parent";
function App() {
return (
<div>
<h3>Nesting of Components</h3>
<Parent />
</div>
);
}
export default App;
Output:
OUTPUT IMAGE FOR NESTING OF COMPONENTSSimilarity between component composition and nesting component
- Increases the reusability of components
- Incorporates modularity in web application
- Increases readability of the code
- It eases the code maintainability
- It leads to easy scalability of the application
- It eases the flow of data across the application
Difference between component composition and nesting component
Aspect | Component Composition | Nesting Components |
---|
Relationship | Combines smaller parts into a larger component | Creates a parent-child relationship |
Flexibility | Offers more flexibility | Can be less flexible due to tight coupling |
Complexity Management | Converts complex problems into simpler ones | Can become complex, especially in large apps |
Reusability | Promotes reusability of smaller components | Utilizes hierarchical structure for reusability |
Code Structure | Provides a modular architecture | Creates a structured hierarchy |
Maintenance | Simplifies maintenance with modular components | Requires careful management of parent-child relationship |
Similar Reads
ReactJS Component Composition In React, it becomes difficult to manage the complex UI components and maintain the code. With the help of Component Compositions, we can solve this problem by combining the components into a single component.What is Component Composition?Component composition in React includes one or more component
5 min read
How to Call Parent Components's Function from Child Component in React ? In React, it is very important to manage communication between components for building flexible and maintainable applications. Sometime, you need to trigger a function defined in a parent component from a child component. In this article, we'll see how to achieve this in React by passing down functi
3 min read
How to embed two components in one component ? React allows us to render one component inside another component. It means we can create the parent-child relationship between the 2 or more components. In the ReactJS Components, it is easy to build a complex UI of any application. We can divide the UI of the application into small components and r
2 min read
React.js Blueprint HTML Elements Component Nested Usage React.js Blueprint is a front-end UI toolkit. It is very optimized and popular for building interfaces that are complex data-dense for desktop applications. We can add styles and the features of React.js BluePrint HTML elements Component to the HTML elements just by providing a className {Classes.R
2 min read
React Importing and Exporting Components In React, components are the building blocks of any application. They allow developers to break down complex user interfaces into smaller, reusable pieces. To manage these components efficiently, you need to understand how to import and export them.In this article, we will explore the different meth
6 min read
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