What are Higher Order Components?

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
Search for a command to run...

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
No comments yet. Be the first to comment.
Add it to the boot method of the AppServiceProvider of the app to improve Laravel developmet experience. use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; class AppServiceProvider extends ServiceProvider { //... pub...

There is a plugin available for reactjs that one can use to add Calendly to Nextjs i.e., react-calendly. The problem is if you want to customize the button to open popup or modal then you need to have Calendly's pro plan for it. Here is the alternati...

After shifting to Ubuntu from Windows, I constantly look up for the alternatives of the tools that are available in Windows to enhance my arsenal in Linux. DbGate is the smartest SQL+noSQL database client. It works on Windows, Linux, MacOS and in web...

<div className="ratio ratio-16x9"> <video width="900" height="650" muted playsInline autoPlay loop> <source src="/images/test.mp4" type="video/mp4" /> </video> </div>

Creating middleware in Laravel is a common task that helps to filter HTTP requests entering your application. Middleware can perform various tasks such as authentication, logging, and modifying request/response objects. Step 1: Create Middleware You ...

In React, higher-order components (HOCs) are functions that take a component as input and return an enhanced version of that component. They are a way to reuse component logic and share common functionality across multiple components.
In functional components, you can use higher-order components by utilizing the concept of "function composition." Here's an example of how you can create and use an HOC in a functional component:
import React, { useState, useEffect } from 'react';
const withLoading = (WrappedComponent) => {
const EnhancedComponent = (props) => {
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
// Simulating an API call delay
const delay = setTimeout(() => {
setIsLoading(false);
}, 2000);
return () => clearTimeout(delay);
}, []);
if (isLoading) {
return <div>Loading...</div>;
}
return <WrappedComponent {...props} />;
};
return EnhancedComponent;
};
const DataComponent = ({ data }) => {
return (
<ul>
{data.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
};
const EnhancedDataComponent = withLoading(DataComponent);
const App = () => {
const data = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' },
];
return <EnhancedDataComponent data={data} />;
};
export default App;
In this example, the withLoading HOC adds a loading state (isLoading) to the wrapped component. When the component is rendered, it starts in a loading state and displays the "Loading..." message. After a simulated delay of 2 seconds, the loading state is set to false, and the wrapped component (DataComponent) is rendered with the provided data.
You can use the EnhancedDataComponent in your app, and it will automatically handle the loading state and display the loading spinner while the data is being fetched.
This is just one example of how you can use higher-order components in functional components to add reusable functionality. HOCs can be used for various purposes, such as handling authentication, data fetching, styling, and more. The key is to identify reusable logic that can be encapsulated within an HOC and applied to multiple components in your application.