Bird
Raised Fist0
Vueframework~20 mins

Higher-order components concept in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue HOC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this higher-order component render?

Given the following Vue 3 higher-order component (HOC) wrapping a base component, what will be rendered?

Vue
import { defineComponent, h } from 'vue';

const withWrapper = (BaseComponent) => {
  return defineComponent({
    setup() {
      return () => h('div', { class: 'wrapper' }, [h(BaseComponent)]);
    }
  });
};

const Base = defineComponent({
  setup() {
    return () => h('p', 'Hello from Base');
  }
});

const Wrapped = withWrapper(Base);

// Assume <Wrapped /> is used in template
A<div class="wrapper"><p>Hello from Base</p></div>
B<div><p>Hello from Base</p></div>
C<p>Hello from Base</p>
D<div class="base"><p>Hello from Base</p></div>
Attempts:
2 left
💡 Hint

Think about what the HOC adds around the base component.

❓ state_output
intermediate
2:00remaining
How does state behave in this HOC example?

Consider this Vue 3 HOC that adds a counter state to a base component. What is the value of count displayed after clicking the button twice?

Vue
import { defineComponent, ref, h } from 'vue';

const withCounter = (BaseComponent) => {
  return defineComponent({
    setup() {
      const count = ref(0);
      const increment = () => count.value++;
      return () => h('div', [
        h(BaseComponent, { count: count.value }),
        h('button', { onClick: increment }, 'Increment')
      ]);
    }
  });
};

const DisplayCount = defineComponent({
  props: ['count'],
  setup(props) {
    return () => h('p', `Count is: ${props.count}`);
  }
});

const Enhanced = withCounter(DisplayCount);

// Assume <Enhanced /> is rendered and user clicks button twice
ACount is: undefined
BCount is: 0
CCount is: 1
DCount is: 2
Attempts:
2 left
💡 Hint

Each click increases the count by one, starting from zero.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a Vue 3 HOC that adds a class prop?

Which of the following options correctly defines a higher-order component that adds a class prop to the wrapped component?

A
const withClass = (Base) =&gt; defineComponent({
  setup(props, { attrs }) {
    return () =&gt; h(Base, { ...attrs, class: 'added-class' });
  }
});
B
const withClass = (Base) =&gt; defineComponent({
  setup(props, { slots }) {
    return () =&gt; h(Base, { class: 'added-class' }, slots.default());
  }
});
C
const withClass = (Base) =&gt; defineComponent({
  render() {
    return h(Base, { class: 'added-class' });
  }
});
D
const withClass = (Base) =&gt; defineComponent({
  setup(props) {
    return () =&gt; h(Base, { class: 'added-class' });
  }
});
Attempts:
2 left
💡 Hint

Consider how to forward attributes and add a class in setup function.

🔧 Debug
advanced
2:00remaining
Why does this HOC cause a runtime error?

Given this Vue 3 HOC code, why does it cause a runtime error when used?

Vue
import { defineComponent, h } from 'vue';

const withError = (Base) => {
  return defineComponent({
    setup() {
      // Missing return statement
      h(Base);
    }
  });
};
AThe Base component is not imported, causing a reference error.
BThe setup function does not return a render function, causing a runtime error.
CThe h function is used incorrectly with missing arguments.
DThe defineComponent call is missing a name property.
Attempts:
2 left
💡 Hint

Remember what the setup function must return in Vue 3 components.

🧠 Conceptual
expert
2:00remaining
What is a key benefit of using higher-order components in Vue 3?

Which of the following best describes a key benefit of using higher-order components (HOCs) in Vue 3?

AThey enforce strict typing on component props and events at runtime.
BThey automatically optimize component rendering by memoizing outputs based on props changes.
CThey allow reusing component logic by wrapping components and enhancing their behavior without modifying them directly.
DThey replace the need for Vue's Composition API by providing an alternative syntax for reactive state.
Attempts:
2 left
💡 Hint

Think about how HOCs help with code reuse and separation of concerns.

Practice

(1/5)
1. What is the main purpose of a higher-order component (HOC) in Vue?
easy
A. To create a new Vue instance from scratch
B. To replace the Vue router with a new navigation system
C. To wrap a component and add shared logic or styling
D. To directly manipulate the DOM elements inside a component

Solution

  1. Step 1: Understand what HOCs do

    Higher-order components wrap existing components to add extra features without changing the original component.
  2. Step 2: Identify the correct purpose

    Among the options, only wrapping a component to add shared logic or styling matches the HOC concept.
  3. Final Answer:

    To wrap a component and add shared logic or styling -> Option C
  4. Quick Check:

    HOC purpose = wrap and add logic [OK]
Hint: HOCs wrap components to add features without changing them [OK]
Common Mistakes:
  • Confusing HOCs with routing or Vue instance creation
  • Thinking HOCs manipulate DOM directly
  • Believing HOCs replace components instead of wrapping
2. Which of the following is the correct syntax to create a higher-order component in Vue 3 using the Composition API?
easy
A. const withLogger = (WrappedComponent) => { return { setup() { return () => h(WrappedComponent) } } }
B. const withLogger = (WrappedComponent) => { return WrappedComponent.setup() }
C. const withLogger = (WrappedComponent) => { return new Vue({ render: () => h(WrappedComponent) }) }
D. const withLogger = (WrappedComponent) => { return () => WrappedComponent }

Solution

  1. Step 1: Review HOC syntax in Vue 3

    A HOC is a function that returns a new component wrapping the original. Using setup and render function with h() is correct.
  2. Step 2: Check each option

    const withLogger = (WrappedComponent) => { return { setup() { return () => h(WrappedComponent) } } } returns a component object with setup returning a render function that renders WrappedComponent, which is correct.
  3. Final Answer:

    const withLogger = (WrappedComponent) => { return { setup() { return () => h(WrappedComponent) } } } -> Option A
  4. Quick Check:

    HOC returns component with setup and render [OK]
Hint: HOCs return a new component wrapping the original with setup [OK]
Common Mistakes:
  • Calling setup() directly instead of returning a component
  • Using new Vue() in Vue 3 Composition API
  • Returning the component function without wrapping
3. Given this HOC code in Vue 3:
const withBorder = (WrappedComponent) => {
  return {
    setup() {
      return () => h('div', { style: { border: '2px solid red' } }, [h(WrappedComponent)])
    }
  }
}

const Base = { template: '

Hello

' } const Enhanced = withBorder(Base)

What will <Enhanced /> render?
medium
A.

Hello

B.

Hello

C.

Hello

D. Error: h() cannot wrap components

Solution

  1. Step 1: Understand the HOC rendering

    The HOC wraps the WrappedComponent inside a div with a red border style.
  2. Step 2: Predict the rendered output

    Rendering <Enhanced /> produces a div with red border containing the Base component's paragraph.
  3. Final Answer:

    <div style="border: 2px solid red"><p>Hello</p></div> -> Option B
  4. Quick Check:

    HOC adds border div around component [OK]
Hint: HOCs wrap output in extra elements or logic [OK]
Common Mistakes:
  • Ignoring the wrapping div and expecting only <p>
  • Thinking h() cannot wrap components
  • Assuming no styles are applied
4. Identify the error in this Vue 3 HOC code:
const withTitle = (WrappedComponent) => {
  return {
    setup() {
      return () => h(WrappedComponent)
    }
  }
}

const Base = { template: '

Hi

' } const Enhanced = withTitle(Base) // Usage: <Enhanced title="Welcome" />

What is the problem?
medium
A. The HOC does not pass props to WrappedComponent
B. The HOC should use a class component instead
C. The Base component must be a function, not an object
D. The setup function cannot return a render function

Solution

  1. Step 1: Check prop forwarding in HOC

    The HOC returns WrappedComponent but does not forward any props it receives.
  2. Step 2: Understand consequence

    When using <Enhanced title="Welcome" />, the title prop is lost because it's not passed down.
  3. Final Answer:

    The HOC does not pass props to WrappedComponent -> Option A
  4. Quick Check:

    Props must be forwarded in HOCs [OK]
Hint: Always forward props from HOC to wrapped component [OK]
Common Mistakes:
  • Forgetting to pass props in render function
  • Thinking setup can't return render function
  • Believing Base must be a function component
5. You want to create a higher-order component withLoading that shows a loading message while data is loading, then renders the wrapped component with the loaded data. Which approach correctly combines HOC logic with Vue 3 Composition API?
hard
A. Create a mixin that adds loading state and use it inside the wrapped component
B. Modify the wrapped component's template directly to add loading message
C. Use a global variable to track loading and conditionally render wrapped component
D. Return a component with setup() that uses ref for loading state, fetches data, and renders loading or wrapped component with props

Solution

  1. Step 1: Understand HOC with loading logic

    The HOC should manage loading state internally and decide what to render based on it.
  2. Step 2: Identify correct Vue 3 pattern

    Using setup with ref for loading, fetching data, and returning a render function that shows loading or wrapped component with data is correct.
  3. Final Answer:

    Return a component with setup() that uses ref for loading state, fetches data, and renders loading or wrapped component with props -> Option D
  4. Quick Check:

    HOC manages state and renders conditionally [OK]
Hint: Use setup with state and conditional render in HOC [OK]
Common Mistakes:
  • Changing wrapped component template directly
  • Using global variables for state
  • Confusing mixins with HOCs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard