2 min read react
Advanced Component Composition in React
Mastering Advanced Component Composition in React
In the realm of React, component composition isn’t just about assembling simple parts. It’s an art form that, when mastered, can lead to incredibly powerful and flexible applications. Let’s explore a more advanced example of component composition, illustrating how it can handle complex scenarios elegantly.
Setting the Stage: A Contextual Scenario
Imagine we’re building an interactive dashboard for a project management app. This dashboard comprises various components like TaskList
, TaskItem
, and UserAvatar
. We’ll also use a ThemeContext
for consistent styling across our components.
Step 1: Creating Basic Components
First, let’s define our basic components:
const TaskList = ({ tasks }) => (
<ul>
{tasks.map(task => <TaskItem key={task.id} {...task} />)}
</ul>
);
const TaskItem = ({ id, title, assignedTo }) => (
<li>
<span>{title}</span>
<UserAvatar userId={assignedTo} />
</li>
);
const UserAvatar = ({ userId }) => {
const user = /* Fetch user based on userId */;
return <img src={user.avatarUrl} alt={user.name} />;
};
Step 2: Introducing Context for Theme Management
Now, let’s add ThemeContext
to provide consistent theming:
const ThemeContext = React.createContext("light");
const ThemedTaskItem = ({ id, title, assignedTo }) => (
<ThemeContext.Consumer>
{(theme) => (
<li className={`task-item task-item-${theme}`}>
<span>{title}</span>
<UserAvatar userId={assignedTo} />
</li>
)}
</ThemeContext.Consumer>
);
Step 3: Leveraging Higher-Order Components
We’ll create a Higher-Order Component (HOC) to enhance UserAvatar
with theme information:
const withTheme = (Component) => (props) =>
(
<ThemeContext.Consumer>
{(theme) => <Component {...props} theme={theme} />}
</ThemeContext.Consumer>
);
const ThemedUserAvatar = withTheme(UserAvatar);
Now ThemedUserAvatar
is aware of the current theme.
Step 4: Combining Components for the Dashboard
Finally, let’s assemble our dashboard:
const Dashboard = ({ tasks }) => (
<ThemeContext.Provider value="dark">
<TaskList tasks={tasks} />
{/* Now TaskList and all its children have access to the theme context */}
</ThemeContext.Provider>
);
ReactDOM.render(
<Dashboard tasks={sampleTasks} />,
document.getElementById("root")
);
Conclusion: The Power of Advanced Composition
In this example, we saw how React’s component composition can elegantly handle complex scenarios, like theme management across a hierarchy of components. By breaking down the UI into small, focused components and leveraging context and higher-order components, we can build sophisticated and maintainable React applications.
Fun Fact Did you know that React’s composition model was influenced by the principles of functional programming? It emphasizes the creation of small, pure components - similar to how functional programming values small, pure functions!
Remember, the beauty of React lies not just in its simplicity, but also in its power to gracefully manage complexity through advanced component composition.