In React, a higher-order component (HOC) is a function that takes a component as input and returns a new component that wraps the original one. The purpose of a higher-order component is to add some extra functionality or behavior to the wrapped component.
Here are some common use cases for higher-order components in React:
Code reuse: HOCs can help you avoid duplicating code across different components. For example, you might create a HOC that adds authentication or authorization checks to a component.
Prop manipulation: HOCs can modify the props that are passed to a component. For example, you might create a HOC that injects additional props or overrides existing ones.
Render hijacking: HOCs can intercept the rendering process and modify the output. For example, you might create a HOC that adds a loading spinner while data is being fetched.
State management: HOCs can manage the state for a component. For example, you might create a HOC that stores form data in the state and handles form submission.
Conditionally rendering components: HOCs can be used to conditionally render components based on certain criteria. For example, you might create a HOC that renders a component only if the user is authenticated.
Performance optimization: HOCs can help optimize performance by reducing the number of renders that occur. For example, you might create a HOC that memoizes the output of a component if its props haven't changed.
Here's an example of a simple HOC that logs each time a component is rendered:
function withLogger(WrappedComponent) {
return class extends React.Component {
componentDidMount() {
console.log(`Component ${WrappedComponent.name} rendered`);
}
render() {
return <WrappedComponent {...this.props} />;
}
};
}
const MyComponent = () => <div>Hello, world!</div>;
const LoggedComponent = withLogger(MyComponent);
// Renders "Component MyComponent rendered"
ReactDOM.render(<LoggedComponent />, document.getElementById('root'));
In this example, the withLogger
function takes a component (MyComponent
) and returns a new component (LoggedComponent
) that logs each time it's rendered. The LoggedComponent
can be used just like any other React component.
Creating a HIGH - ORDER COMPONENTS
To create a higher-order component (HOC) in React, you can define a function that takes a component as an argument and returns a new component that wraps the original one. Here's an example of a simple HOC that adds an "isLoading" prop to a component:
javascriptCopy codefunction withLoading(Component) {
return function WithLoading(props) {
const [isLoading, setIsLoading] = React.useState(false);
function startLoading() {
setIsLoading(true);
}
function stopLoading() {
setIsLoading(false);
}
return <Component isLoading={isLoading} startLoading={startLoading} stopLoading={stopLoading} {...props} />;
};
}
In this example, the withLoading
function takes a Component
as its argument and returns a new component called WithLoading
. The WithLoading
component has its own state for tracking whether it's loading, as well as functions for starting and stopping loading. It then passes the isLoading
, startLoading
, and stopLoading
props to the wrapped Component
, along with any other props that were passed to it.
To use the withLoading
HOC, you simply need to pass your component to it as an argument:
javascriptCopy codefunction MyComponent(props) {
return (
<div>
<h1>My Component</h1>
{props.isLoading ? <p>Loading...</p> : <p>Content goes here</p>}
</div>
);
}
const WrappedComponent = withLoading(MyComponent);
function App() {
return (
<div>
<h1>App</h1>
<WrappedComponent />
</div>
);
}
ReactDOM.render(<App />, document.getElementById('root'));
In this example, the MyComponent
function is passed to the withLoading
function to create a new component called WrappedComponent
. The WrappedComponent
is then used in the App
component. When the WrappedComponent
is rendered, it will have the isLoading
, startLoading
, and stopLoading
props passed down to it, along with any other props that were passed to it.
Overall, HOCs can be used to solve a variety of problems in your React applications and make your code more modular and reusable.