0Pricing
Vue Academy · Lesson

Higher-Order Components (HOC) Pattern

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

Higher-Order Components (HOC) Pattern is a free Vue Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Higher-Order Components (HOC) Pattern” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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