Performance: Higher-order components concept
This concept affects rendering speed and responsiveness by adding extra component layers in the Vue component tree.
const useLogger = (props) => { watchEffect(() => { console.log('Rendering component with props:', props); }); }; const Button = defineComponent({ props: { label: String }, setup(props) { useLogger(props); return () => h('button', null, props.label); } }); // Usage in template // <Button label="Click me" />
const withLogger = (WrappedComponent) => { return { props: WrappedComponent.props, setup(props, { slots }) { console.log('Rendering component with props:', props); return () => h(WrappedComponent, props, slots.default ? slots.default() : undefined); } }; }; const EnhancedButton = withLogger(Button); // Usage in template // <EnhancedButton label="Click me" />
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| HOC wrapping base component | Increases component nodes by 1 per use | Triggers reactive updates on wrapper and base | Slightly higher due to deeper tree | [!] OK |
| Composable function inside component | No extra component nodes | Reactive updates only on base component | Lower paint cost due to simpler tree | [OK] Good |
const withBorder = (WrappedComponent) => {
return {
setup() {
return () => h('div', { style: { border: '2px solid red' } }, [h(WrappedComponent)])
}
}
}
const Base = { template: 'Hello
' }
const Enhanced = withBorder(Base)<Enhanced /> render?const withTitle = (WrappedComponent) => {
return {
setup() {
return () => h(WrappedComponent)
}
}
}
const Base = { template: 'Hi
' }
const Enhanced = withTitle(Base)
// Usage: <Enhanced title="Welcome" />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?Pass the test and this pattern moves unknown