0
0
Vueframework~20 mins

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

Choose your learning style9 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.