From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Higher order components (HOCs)

Higher order components (HOCs)

In this lecture, we're going to explore another React pattern – higher-order components, or HOCs. HOCs were very popular before hooks, and while they're less common today, you'll still see them in many libraries and codebases, so it's important to understand how they work. So, what is a higher-order component? A higher-order component is essentially a function that takes an existing component and returns a new component with added functionality. Think of it as a wrapper that enhances your components without modifying the original. The main idea here is reusability. Instead of copying the same logic into multiple components, you can wrap them with a higher-order component and share that logic seamlessly. In simple terms, you can imagine it like this. Here, WrappedComponent is your original component and EnhancedComponent is the new one with the extra features. This pattern is powerful for injecting shared behavior across components like logging, theming, or authentication. Let's create…

Contents