Vue.js course · Module 14: Directives & Plugins
Renderless Components and Functional Patterns
In this lesson6
In the NOVA LAB Command Center, not all interface modules need a visual representation. Some are responsible solely for logic - these are renderless components. Let's explore advanced Vue component patterns.
What Is a Renderless Component?
A renderless component is a component that does not render its own HTML - instead, it exposes logic through a slot. The parent component defines all the visual appearance.
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
// Renderless component - clock logic
const now = ref(new Date())
let timer = null
onMounted(() => {
timer = setInterval(() => {
now.value = new Date()
}, 1000)
})
onUnmounted(() => clearInterval(timer))
defineExpose({ now })
</script>
<template>
<slot :now="now" :formatted="now.toLocaleTimeString('en-US')" />
</template>Usage:
<template>
<Clock v-slot="{ formatted }">
<div class="station-clock">{{ formatted }}</div>
</Clock>
</template>The Clock component has no HTML of its own - it passes data through a scoped slot, and the parent decides on the appearance.
Renderless Pattern with Toggle Logic
A popular pattern - a component managing toggle state:
<!-- Toggle.vue -->
<script setup>
import { ref } from 'vue'
const isOn = ref(false)
function toggle() {
isOn.value = !isOn.value
}
function setOn() { isOn.value = true }
function setOff() { isOn.value = false }
</script>
<template>
<slot :isOn="isOn" :toggle="toggle" :setOn="setOn" :setOff="setOff" />
</template>Usage in different contexts:
<template>
<!-- As a switch -->
<Toggle v-slot="{ isOn, toggle }">
<button @click="toggle">
System: {{ isOn ? 'ACTIVE' : 'OFFLINE' }}
</button>
</Toggle>
<!-- As an expandable panel -->
<Toggle v-slot="{ isOn, toggle }">
<div>
<h3 @click="toggle">Diagnostics ▼</h3>
<div v-if="isOn">System details...</div>
</div>
</Toggle>
</template>Functional Components
In Vue 3, every component with <script setup> is already optimized. But you can also create simple functional components as plain render functions:
import { h } from 'vue'
// Functional component - no state, only props
function StationBadge(props) {
return h('span', {
class: 'badge',
style: {
color: props.color || '#00ff88',
border: '1px solid ' + (props.color || '#00ff88'),
padding: '4px 12px',
borderRadius: '12px',
fontSize: '12px'
}
}, props.label)
}
StationBadge.props = ['label', 'color']Usage in a template:
<template>
<StationBadge label="ONLINE" color="#2ecc71" />
<StationBadge label="ALERT" color="#e74c3c" />
</template>Render Functions with h()
The h() function (hyperscript) creates a VNode - a virtual DOM element:
import { h } from 'vue'
// h(tag, props, children)
h('div', { class: 'panel' }, [
h('h2', null, 'Title'),
h('p', null, 'Panel content')
])A component with a render function instead of a template:
import { h, ref, defineComponent } from 'vue'
const Counter = defineComponent({
setup() {
const count = ref(0)
return () => h('div', { class: 'counter' }, [
h('p', null, 'Counter: ' + count.value),
h('button', {
onClick: () => count.value++
}, '+1')
])
}
})Higher-Order Components (HOC)
An HOC is a function that takes a component and returns a new component with added functionality:
import { h, ref, onMounted } from 'vue'
function withLoading(WrappedComponent) {
return defineComponent({
setup(props, { attrs }) {
const loading = ref(true)
onMounted(() => {
setTimeout(() => {
loading.value = false
}, 1000)
})
return () => {
if (loading.value) {
return h('div', { class: 'loading' }, 'Loading module...')
}
return h(WrappedComponent, { ...attrs })
}
}
})
}Usage:
const MissionPanelWithLoading = withLoading(MissionPanel)When to Use These Patterns?
| Pattern | When to Use |
|---|---|
| Renderless component | Reusable logic with flexible UI |
| Functional component | Simple, stateless presentational elements |
| Render function (h()) | Dynamic DOM structure generation |
| HOC | Adding behavior to existing components |
Renderless components and scoped slots are Vue's idiomatic way of separating logic from presentation - especially useful in systems as complex as the NOVA LAB Command Center.
Code for this lesson: App.vue
<script setup>
import { ref, h, defineComponent } from 'vue'
// Renderless Toggle component
const Toggle = defineComponent({
setup(_, { slots }) {
const isOn = ref(false)
function toggle() { isOn.value = !isOn.value }
function setOn() { isOn.value = true }
function setOff() { isOn.value = false }
return () => slots.default?.({
isOn: isOn.value,
toggle,
setOn,
setOff
})
}
})
// Functional component
function StationBadge(props) {
return h('span', {
style: {
color: props.color || '#00ff88',
border: '1px solid ' + (props.color || '#00ff88'),
padding: '4px 12px',
borderRadius: '12px',
fontSize: '12px',
display: 'inline-block'
}
}, props.label)
}
StationBadge.props = ['label', 'color']
// Counter with render function
const Counter = defineComponent({
setup() {
const count = ref(0)
return () => h('div', { class: 'counter-box' }, [
h('span', null, 'Counter: ' + count.value),
h('button', {
onClick: () => count.value++,
class: 'btn small',
style: 'margin-left: 10px'
}, '+1'),
h('button', {
onClick: () => count.value = 0,
class: 'btn small reset',
style: 'margin-left: 6px'
}, 'Reset')
])
}
})
</script>
<template>
<div class="nova-lab">
<h1>Renderless Components i Functional Patterns</h1>
<section class="section">
<h2>Renderless Toggle</h2>
<Toggle v-slot="{ isOn, toggle }">
<div>
<button @click="toggle" :class="['btn', { active: isOn }]">
System: {{ isOn ? 'ACTIVE' : 'OFF' }}
</button>
<div v-if="isOn" class="panel">
<p>Diagnostic systems running.</p>
<p>Temperature: 22.5°C | Pressure: 1.01 atm</p>
</div>
</div>
</Toggle>
</section>
<section class="section">
<h2>Renderless Toggle - another use</h2>
<Toggle v-slot="{ isOn, toggle }">
<div>
<h3 @click="toggle" class="expandable">
{{ isOn ? '▼' : '▶' }} Station logs
</h3>
<ul v-if="isOn" class="log-list">
<li>[09:15] Navigation system OK</li>
<li>[09:16] Sector scan completed</li>
<li>[09:17] Data synchronization</li>
</ul>
</div>
</Toggle>
</section>
<section class="section">
<h2>Functional Component - StationBadge</h2>
<div class="badge-row">
<StationBadge label="ONLINE" color="#2ecc71" />
<StationBadge label="ALERT" color="#e74c3c" />
<StationBadge label="STANDBY" color="#f39c12" />
<StationBadge label="NOVA LAB" color="#00b4d8" />
</div>
</section>
<section class="section">
<h2>Render Function - Counter</h2>
<Counter />
</section>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #c0c0c0;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 20px; }
h2 { color: #00b4d8; font-size: 16px; margin-bottom: 10px; }
h3 { color: #00ff88; margin: 0; }
.section {
background: rgba(0, 180, 216, 0.05);
border: 1px solid rgba(0, 180, 216, 0.2);
padding: 15px;
border-radius: 8px;
margin-bottom: 15px;
}
.btn {
background: rgba(0,255,136,0.15);
color: #00ff88;
border: 1px solid #00ff88;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-family: monospace;
}
.btn.active { background: #00ff88; color: #0a0e27; }
.btn.small { font-size: 12px; padding: 4px 10px; }
.btn.reset { color: #e74c3c; border-color: #e74c3c; }
.panel {
margin-top: 10px;
padding: 10px;
background: rgba(0,255,136,0.05);
border: 1px solid rgba(0,255,136,0.2);
border-radius: 6px;
}
.panel p { margin: 4px 0; font-size: 13px; }
.expandable { cursor: pointer; user-select: none; }
.expandable:hover { color: #00b4d8; }
.log-list {
list-style: none;
padding: 0;
margin: 8px 0 0;
}
.log-list li {
padding: 4px 0;
font-size: 13px;
color: #888;
border-bottom: 1px solid rgba(0,180,216,0.1);
}
.badge-row { display: flex; gap: 10px; flex-wrap: wrap; }
.counter-box {
display: flex;
align-items: center;
padding: 10px;
background: rgba(0,255,136,0.05);
border-radius: 6px;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is a renderless component in Vue?
2. How does a renderless component pass data to the parent component?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Implement a Toggle component with ref isOn, a toggle function, and a scoped slot.
- Vertical ordering
Arrange the steps for creating a renderless component in the correct order:
- Click in order
Arrange the correct order of arguments for the h() function:
- Code editor
Implement StationBadge(props) as a function returning h('span', ...) with props.label and props.color.
- Horizontal ordering
Arrange the correct import of the h function from Vue:
- Code editor
Implement a Counter with ref count and a render function returning h() with +1 and Reset buttons.
- Vertical ordering
Arrange the patterns from simplest to most advanced:
Tasks after this lesson
- QuestionRenderless component
- QuestionScoped slots in a renderless component
- Coding taskPractice: Renderless Toggle
- Put in orderRenderless component pattern
- QuestionThe h() function in Vue
- Click in orderArguments of the h() function
- Coding taskPractice: Functional Component
- Put in orderImporting the h() function
- QuestionHigher-Order Component
- Coding taskPractice: Render Function with h()
- Put in orderWhen to use advanced patterns