📖 Scenario: You are creating a reusable logic component in Vue 3 that does not render any HTML itself but provides data and behavior to other components.This pattern is called a renderless component and helps separate logic from UI.
🎯 Goal: Build a renderless component called useCounter that manages a counter state and exposes increment and decrement functions.Then use this component inside a parent component to display and control the counter.
📋 What You'll Learn
Create a renderless component using the Composition API with setup().
Use ref to create a reactive counter variable starting at 0.
Provide increment and decrement functions to change the counter.
Use the renderless component inside a parent component and display the counter value.
Add buttons in the parent component to call increment and decrement.
💡 Why This Matters
🌍 Real World
Renderless components help you reuse logic across multiple UI components without forcing a specific look or markup. This is useful in design systems and component libraries.
💼 Career
Understanding renderless components is important for Vue developers building scalable, maintainable apps and libraries that separate logic from presentation.
Progress0 / 4 steps
1
Create the renderless component with a reactive counter
Create a Vue 3 renderless component named useCounter using the Composition API. Inside setup(), create a reactive variable called count initialized to 0 using ref. Return an object containing count.
Vue
Hint
Use ref(0) to create a reactive counter variable inside setup().
2
Add increment and decrement functions to the renderless component
Inside the useCounter component, add two functions named increment and decrement. increment should add 1 to count.value. decrement should subtract 1 from count.value. Return both functions along with count.
Vue
Hint
Define increment and decrement functions that update count.value.
3
Use the renderless component inside a parent component
Create a Vue 3 parent component named CounterDisplay. Inside its setup(), call useCounter() and destructure count, increment, and decrement. Return these. In the template, display the count value inside a <span>. Add two buttons labeled + and - that call increment and decrement on click.
Vue
Hint
Use useCounter() inside setup() and bind the functions to buttons.
4
Complete the parent component with accessibility and styling
In the CounterDisplay component template, add aria-label attributes to the increment and decrement buttons with values "Increase count" and "Decrease count" respectively. Add a role="region" and aria-live="polite" to the <span> showing the count for screen readers.
Vue
Hint
Add accessibility attributes to buttons and the count display for screen reader support.
Practice
(1/5)
1. What is the main purpose of a renderless component in Vue?
easy
A. To share logic without rendering any UI elements
B. To render a default UI for all child components
C. To replace the Vue router for navigation
D. To automatically fetch data from APIs
Solution
Step 1: Understand the concept of renderless components
Renderless components are designed to provide reusable logic without producing any visible UI themselves.
Step 2: Compare options with the concept
Options A, B, and D describe unrelated Vue features or behaviors. Only To share logic without rendering any UI elements correctly describes the purpose of renderless components.
Final Answer:
To share logic without rendering any UI elements -> Option A
Quick Check:
Renderless components = share logic only [OK]
Hint: Renderless means no UI, just logic sharing [OK]
Common Mistakes:
Thinking renderless components render UI
Confusing with routing or data fetching
Assuming they provide default templates
2. Which syntax correctly defines a renderless component using slots in Vue 3?
easy
A. <template><slot :data="someData" /></template>
B. <template><div>Hello</div></template>
C. <script>export default { render() { return null } }</script>
D. <template><button>Click</button></template>
Solution
Step 1: Identify renderless component pattern
Renderless components use slots to pass data and methods without rendering UI elements themselves.
Step 2: Analyze each option
<template><slot :data="someData" /></template> uses a slot with a prop to pass data, which is the correct renderless pattern. Options A and D render UI elements, and C returns null but does not use slots.
Final Answer:
<template><slot :data="someData" /></template> -> Option A
Quick Check:
Renderless = slot with data prop [OK]
Hint: Renderless components pass data via slots, no UI tags [OK]
Common Mistakes:
Including UI elements inside renderless components
A. Template must have a root element wrapping slot
B. Slot usage is invalid without a name
C. Increment function incorrectly updates count with count++ instead of count.value++
D. Ref is not imported from Vue
Solution
Step 1: Check reactive ref usage
In Vue 3, refs must be updated via count.value++, not count++.
Step 2: Verify other parts
Slot usage without a name is valid for default slot. Template can have a single root slot. Ref is properly imported from Vue.
Final Answer:
Increment function incorrectly updates count with count++ instead of count.value++ -> Option C
Quick Check:
Update ref with .value [OK]
Hint: Use .value to update refs in Vue 3 [OK]
Common Mistakes:
Updating ref directly without .value
Assuming slot needs a name
Thinking template must wrap slot in div
5. You want to create a renderless component that tracks mouse position and shares it with any parent component. Which approach correctly implements this?
hard
A. <template><div>Mouse at {{ x }}, {{ y }}</div></template>
<script setup>
const x = ref(0)
const y = ref(0)
window.addEventListener('mousemove', e => {
x.value = e.clientX
y.value = e.clientY
})
</script>
B. <template><slot :pos="position" /></template>
<script setup>
import { reactive } from 'vue'
const position = reactive({ x: 0, y: 0 })
window.addEventListener('mousemove', e => {
position.x = e.clientX
position.y = e.clientY
})
</script>
C. <template><slot /></template>
<script setup>
import { reactive } from 'vue'
const position = reactive({ x: 0, y: 0 })
window.addEventListener('mousemove', e => {
position.x = e.clientX
position.y = e.clientY
})
</script>
D. <template><slot :x="x" :y="y" /></template>
<script setup>
let x = 0
let y = 0
window.addEventListener('mousemove', e => {
x = e.clientX
y = e.clientY
})
</script>
Solution
Step 1: Identify renderless component pattern
The component must not render UI but expose reactive data via slot props.
Step 2: Analyze each option for correctness
<template><slot :x="x" :y="y" /></template>
<script setup>
let x = 0
let y = 0
window.addEventListener('mousemove', e => {
x = e.clientX
y = e.clientY
})
</script> uses non-reactive plain variables, so position won't update reactively. <template><div>Mouse at {{ x }}, {{ y }}</div></template>
<script setup>
const x = ref(0)
const y = ref(0)
window.addEventListener('mousemove', e => {
x.value = e.clientX
y.value = e.clientY
})
</script> renders UI, so not renderless. <template><slot /></template>
<script setup>
import { reactive } from 'vue'
const position = reactive({ x: 0, y: 0 })
window.addEventListener('mousemove', e => {
position.x = e.clientX
position.y = e.clientY
})
</script> exposes slot without props, so no data passed. <template><slot :pos="position" /></template>
<script setup>
import { reactive } from 'vue'
const position = reactive({ x: 0, y: 0 })
window.addEventListener('mousemove', e => {
position.x = e.clientX
position.y = e.clientY
})
</script> exposes reactive position object via slot prop, clean and correct.
Final Answer:
Expose reactive position object via slot prop without rendering UI -> Option B
Quick Check:
Renderless = slot with reactive data [OK]
Hint: Expose reactive data via slot props, no UI elements [OK]
Common Mistakes:
Rendering UI inside renderless component
Not passing data via slot props
Passing data but not reactive or incomplete
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.