Vue Academy · Lesson

Higher-Order Components (HOC) Pattern

Wrapping components with HOCs, forwarding props and slots, when to use HOC vs composable.

Lesson 4 of 413 steps

Higher-Order Components (HOC) Pattern is a free Vue Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.

What is a Higher-Order Component

A Higher-Order Component (HOC) is a function that takes a component and returns a new component wrapping it. It adds cross-cutting behavior such as logging, auth checks, or data loading without modifying the original.

The Function Signature

An HOC is named with a with prefix by convention. It receives the wrapped component and returns an enhanced one.

function withLogging(Wrapped) {
  // return a new component
}

Returning defineComponent

The new component is built with defineComponent. Its setup function renders the wrapped component via h.

import { defineComponent, h } from "vue";

function withLogging(Wrapped) {
  return defineComponent({
    setup(props, ctx) {
      return () => h(Wrapped, props, ctx.slots);
    }
  });
}

Forwarding Attributes

So the wrapper is transparent, spread ctx.attrs onto the wrapped component. This passes through class, style, and any unrecognized props.

setup(props, { attrs, slots }) {
  return () => h(Wrapped, { ...attrs }, slots);
}

Forwarding Slots

Pass the wrapper's slots object as the children argument so the wrapped component still receives all slot content from the consumer.

return () => h(Wrapped, attrs, slots);

Adding Cross-Cutting Behavior

The whole point is to inject behavior. Here we log mount and unmount around the wrapped component.

import { onMounted, onUnmounted } from "vue";

function withLogging(Wrapped) {
  return defineComponent({
    setup(props, { attrs, slots }) {
      onMounted(() => console.log("mounted"));
      onUnmounted(() => console.log("unmounted"));
      return () => h(Wrapped, attrs, slots);
    }
  });
}

Using the HOC

Wrap any component and use the result like a normal component in templates or other render functions.

import BaseTable from "./BaseTable.vue";

const LoggedTable = withLogging(BaseTable);
// register or use LoggedTable like any component

Naming the Wrapper

Set a name so the wrapper is identifiable in Vue DevTools. Composing the wrapped name keeps the tree readable.

return defineComponent({
  name: "WithLogging(" + (Wrapped.name || "Anonymous") + ")",
  setup(props, ctx) { /* ... */ }
});

Injecting Extra Props

An HOC can supply additional props to the wrapped component, such as a fetched data set or a feature flag.

function withUser(Wrapped) {
  return defineComponent({
    setup(props, { attrs, slots }) {
      const user = inject("currentUser");
      return () => h(Wrapped, { ...attrs, user }, slots);
    }
  });
}

HOC vs Composables

Composables share logic and are often preferred in Vue 3. HOCs are best when you must wrap the rendering of an existing component you cannot edit, or to standardize a boundary like error handling.

Composing Multiple HOCs

Because each HOC returns a component, they compose by nesting. Apply them outermost-last.

const Enhanced = withLogging(withUser(BaseTable));

Quick Check

For an HOC wrapper to be transparent, what must it forward to the wrapped component?

Recap

An HOC like withLogging(Wrapped) returns a new defineComponent whose setup renders the wrapped component with h(Wrapped, attrs, slots), adding cross-cutting behavior such as logging or injected props. Forward attrs and slots for transparency, and prefer composables when you only need to share logic.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
42
Lessons
156

Frequently Asked Questions

Is the “Higher-Order Components (HOC) Pattern” lesson free?

Yes — any 3 lessons of the Vue Academy learning path, including “Higher-Order Components (HOC) Pattern”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.

What will I learn in “Higher-Order Components (HOC) Pattern”?

Wrapping components with HOCs, forwarding props and slots, when to use HOC vs composable. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Higher-Order Components (HOC) Pattern” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The h() Function Deep Dive
  2. JSX in Vue 3 with Vite
  3. Dynamic Components with render Functions
  4. Higher-Order Components (HOC) Pattern
← Back to Vue Academy