In React, composition refers to the practice of building complex user interfaces by combining smaller, reusable components. By breaking down a user interface into smaller pieces, each with a single responsibility, you can create more maintainable and scalable code.
Here are some tips for composition in React:
Start by identifying the individual pieces of your UI. Each piece should have a clear and specific responsibility.
Build each piece as a standalone React component, with its props and state.
Use props to pass data and callbacks between components. This makes it easy to reuse components in different contexts.
Use composition to combine smaller components into larger ones. This can be done with the help of a container component that renders child components and manages their state.
Keep components as simple and focused as possible. This makes them easier to test, debug, and maintain.
Consider using higher-order components (HOCs) to share functionality between components. HOCs are functions that take a component as input and return a new component with additional functionality.
Use the React Context API to provide data to multiple levels of nested components without having to pass props down manually.
By following these best practices, you can create more modular, reusable, and maintainable code in React.
Here are some examples of how composition works in React:
Example of composition using a container component:
import React from 'react'; import Header from './Header'; import Content from './Content'; import Footer from './Footer'; class Page extends React.Component { state = { content: '' } handleContentChange = (content) => { this.setState({ content }); } render() { return ( <div> <Header /> <Content content={this.state.content} onChange={this.handleContentChange} /> <Footer /> </div> ); } } export default Page;
In this example, the
Page
component acts as a container that renders three child components:Header
,Content
, andFooter
. TheContent
component receives data (content
) and a callback function (onChange
) from thePage
component, which uses to update the state of thePage
component.Example of composition using a higher-order component (HOC):
import React from 'react'; const withCount = (WrappedComponent) => { return class extends React.Component { state = { count: 0 } handleIncrement = () => { this.setState({ count: this.state.count + 1 }); } render() { return ( <WrappedComponent count={this.state.count} onIncrement={this.handleIncrement} {...this.props} /> ); } } } const Counter = ({ count, onIncrement }) => { return ( <div> <p>Count: {count}</p> <button onClick={onIncrement}>Increment</button> </div> ); } export default withCount(Counter);
In this example, the
withCount
function is a higher-order component that takes a component (WrappedComponent
) as input and returns a new component that has additional functionality (in this case, acount
state and anonIncrement
callback). TheCounter
component is then wrapped in thewithCount
HOC, which provides it with the additional functionality.
These are just a few examples of how composition works in React. By breaking down a UI into smaller, reusable components and using props, state, and HOCs to combine them, you can create more modular and maintainable code.