BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutRSSGitHubX
Posts/react
Intermediate2 min read

React Higher-Order Components (HOC)

Understand Higher-Order Components (HOCs) in React with examples, use cases, downsides, and a comparison to hooks.

Mohsen Fallahnejad

Mohsen Fallahnejad

Sep 21, 2025Discuss

A Higher-Order Component (HOC) is a function that takes a component and returns a new component with extra features.


1. Analogy ☕#

Think of a coffee cup (your component).
A HOC is like a sleeve you put around the cup: it doesn’t change the coffee, but it adds insulation (extra functionality).


2. Why Use HOCs?#

  • Reusability → extract and share common logic.
  • Separation of concerns → UI components stay simple, HOCs handle cross-cutting logic.
  • Composition → wrap components with multiple behaviors.

3. Example#

tsx
// HOC that adds a loading spinner
function withLoading<P>(Component: React.ComponentType<P>) {
  return function WrappedComponent(props: P & { loading: boolean }) {
    if (props.loading) {
      return <div>Loading...</div>
    }
    return <Component {...props} />
  }
}

// Usage
function UserProfile({ name }: { name: string }) {
  return <h2>{name}</h2>
}

const UserProfileWithLoading = withLoading(UserProfile)

// Render
<UserProfileWithLoading loading={true} name="Alice" />

👉 If loading is true → show spinner. Otherwise, show UserProfile.


4. Common Use Cases#

  • Auth wrapper → redirect if not logged in.
  • Analytics wrapper → log component views.
  • Data fetching → provide fetched data as props.
  • UI wrappers → error boundaries, animations, styles.

5. Downsides#

  • Can cause wrapper hell (too many nested HOCs).
  • Makes React DevTools component tree messy.
  • Harder to type with TypeScript.
  • Modern React often prefers Hooks instead.

6. HOCs vs Hooks#

FeatureHOCs ✅Hooks ✅
Share logic across comps✔️ Yes✔️ Yes
Simple composition❌ Can get messy✔️ Very clean
Works in class components✔️ Yes❌ No (hooks = function comps only)
DevTools readability❌ No✔️ Clear
Modern React preference⚠️ Legacy pattern✔️ Recommended

7. Quick Mental Model 🧠#

  • HOC = function wrapper → adds behavior → returns new component.
  • Use it for auth, logging, theming, analytics wrappers.
  • Prefer hooks for most new code.

⚡ That’s it! HOCs are still useful in some cases, but hooks are the modern go-to for sharing React logic.

Filed under#react#hoc#patterns#frontend

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderJavaScript's Event Loop — Simple Guide (with SVG Timeline)Newer →JavaScript Event Bus 🚍 (JS + TypeScript)

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

React useEffect Dependencies — The Bug That Keeps Coming Back

Why your effect reads old state, fires twice, or loops forever — and the simple rules to fix it (interview-ready).

Jul 8, 20266 min read1

React memo vs useMemo vs useCallback — When to Use Which

Three React APIs that all say 'memoize' — but they do different things. Clear explanations, examples, and a cheatsheet.

Jun 28, 20263 min read

What is 'use client' in Next.js?

Understand 'use client' in Next.js App Router: what it means, when to use it, and how it works under the hood.

Sep 23, 20252 min read

Get new posts in your inbox

No spam. Unsubscribe any time.

2 min left
Discuss👍 React
👍