This document explains the principles and practical use of component composition in React, including abstraction, reusability, hierarchy, props children, and higher-order components.
This document explores how React component composition enables building complex UIs from smaller, reusable function components. It covers abstraction, reusability, hierarchy, props, children, and higher-order components, with practical examples for clarity.
Component composition is the process of combining multiple smaller components to create more complex functionality and larger UI structures. This approach allows developers to build applications using small, focused building blocks.
Below is a practical example demonstrating these principles. Each component encapsulates a specific feature of a blog post, such as title, author, type, and description.
1import React from 'react'
2
3function Title({ text }) {
4 return <h1>{text}</h1>
5}
6
7export default Title
1import React from 'react'
2
3function Author({ name }) {
4 return <h2>By: {name}</h2>
5}
6
7export default Author
1import React from 'react'
2
3function Type({ genre }) {
4 return <div>Type: {genre}</div>
5}
6
7export default Type
1import React from 'react'
2
3function Description({ text }) {
4 return <p>{text}</p>
5}
6
7export default Description
This component composes the above components to create a complete blog post UI.
1import React from 'react'
2import Title from './Title'
3import Author from './Author'
4import Type from './Type'
5import Description from './Description'
6
7function NovelBlog({ title, author, genre, description }) {
8 return (
9 <div className="novel-blog">
10 <Title text={title} />
11 <Author name={author} />
12 <Type genre={genre} />
13 <Description text={description} />
14 </div>
15 )
16}
17
18export default NovelBlog
The main app imports and uses the composed NovelBlog component.
1import React from 'react'
2import NovelBlog from './NovelBlog'
3
4function App() {
5 return (
6 <NovelBlog
7 title="The Great Gatsby"
8 author="F. Scott Fitzgerald"
9 genre="Classic Fiction"
10 description="A novel set in the Roaring Twenties, exploring themes of wealth, love, and the American Dream."
11 />
12 )
13}
14
15export default App
Component composition in React enables developers to build complex UIs by combining small, focused components. By following principles like abstraction, reusability, hierarchy, and using higher-order components, applications become more modular, maintainable, and scalable.
(1) Component composition is about combining smaller components to create complex UIs.
(3) HOCs do not directly render UI; they return enhanced components.
| Principle | Description |
|---|---|
| A. Abstraction | 1. Arranging components in parent-child order |
| B. Hierarchy | 2. Encapsulating UI features in reusable units |
| C. Reusability | 3. Using components in multiple places |
A-2, B-1, C-3.
Higher-order components (HOCs) can enhance a component’s functionality without changing its original implementation.
True. HOCs add features by wrapping or returning enhanced components.