0
0
Vueframework~10 mins

Higher-order components concept in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Higher-order components concept
Start with Base Component
Create HOC Function
HOC Takes Base Component
Return New Component Wrapping Base
Use New Wrapped Component in App
Render Wrapped Component with Added Behavior
The flow shows how a higher-order component (HOC) takes a base component, wraps it with extra features, and returns a new component used in the app.
Execution Sample
Vue
import { h } from 'vue';
const withLogger = (Wrapped) => {
  return {
    setup() {
      console.log('Component mounted');
      return () => h(Wrapped);
    }
  };
};
This code defines a HOC that logs a message when the wrapped component mounts and renders the wrapped component.
Execution Table
StepActionEvaluationResult
1Call withLogger with BaseComponentwithLogger(BaseComponent)Returns new component wrapping BaseComponent
2Render new componentsetup() runsLogs 'Component mounted' to console
3Render function returns h(BaseComponent)BaseComponent rendered inside wrapperBaseComponent UI appears on screen
4User interacts with componentNo change in HOC behaviorBaseComponent handles interaction as usual
5Component unmountsNo special cleanup in HOCComponent removed from DOM
💡 Rendering completes; HOC adds logging but does not alter base component's UI or lifecycle beyond mount log.
Variable Tracker
VariableStartAfter 1After 2Final
WrappedBaseComponentBaseComponentBaseComponentBaseComponent
Returned ComponentundefinedHOC wrapping BaseComponentHOC wrapping BaseComponentHOC wrapping BaseComponent
Key Moments - 3 Insights
Why does the HOC log 'Component mounted' only once?
Because the log happens inside the setup() function which runs once when the component mounts, as shown in execution_table step 2.
Does the HOC change the UI of the base component?
No, the HOC returns a render function that simply renders the base component, so the UI stays the same (execution_table step 3).
Can the HOC add new props or behavior to the base component?
Yes, HOCs can add props or wrap behavior, but in this example it only logs on mount without changing props (see code and execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe user interacts with the component
BThe base component is unmounted
CThe HOC logs 'Component mounted' to the console
DThe HOC returns the base component without rendering
💡 Hint
Check the 'Action' and 'Result' columns in execution_table row 2
At which step does the base component actually render inside the HOC?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for when the render function returns h(BaseComponent) in execution_table
If the HOC added a prop to the base component, how would the execution table change?
AStep 3 would show the base component rendered with extra props
BStep 2 would log a different message
CStep 5 would show cleanup of added props
DStep 1 would not call withLogger
💡 Hint
Adding props affects rendering, so check the 'Evaluation' and 'Result' in step 3
Concept Snapshot
Higher-order components (HOCs) are functions that take a base Vue component and return a new component wrapping it.
They add extra behavior or props without changing the original component code.
HOCs use the setup() function to add logic like logging or state.
The returned component renders the wrapped component inside.
Use HOCs to reuse logic across components cleanly.
Full Transcript
Higher-order components in Vue are functions that accept a base component and return a new component that wraps it. This wrapping component can add extra behavior, such as logging when the component mounts. The example code defines a withLogger HOC that logs a message inside the setup function and renders the wrapped component. The execution table shows the steps: calling the HOC returns a new component, rendering triggers the setup and logs the message, then the base component renders inside the wrapper. Variables track the wrapped component and the returned component. Key moments clarify that the HOC logs only on mount, does not change the UI, and can add behavior or props if desired. The visual quiz tests understanding of these steps. The snapshot summarizes that HOCs help reuse logic by wrapping components without modifying them directly.