Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/template-functions-no-side-effects.md
1---2title: Template Functions Must Be Pure Without Side Effects3impact: MEDIUM4impactDescription: Functions with side effects in templates cause unpredictable behavior on every re-render5type: efficiency6tags: [vue3, template, functions, performance, side-effects]7---89# Template Functions Must Be Pure Without Side Effects1011**Impact: MEDIUM** - Functions called in templates execute on every component re-render. Functions with side effects (modifying data, API calls, logging) will cause unpredictable behavior, performance issues, and difficult-to-debug bugs.1213Template expressions including function calls are evaluated whenever the component updates. This makes them unsuitable for operations that should only happen once or that modify state.1415## Task Checklist1617- [ ] Keep template functions pure (same input = same output)18- [ ] Never modify reactive state inside template functions19- [ ] Never make API calls or async operations in template functions20- [ ] Move side effects to event handlers, watchers, or lifecycle hooks21- [ ] Use computed properties for derived values instead of functions when possible22- [ ] Avoid expensive computations; use computed properties for caching2324**Incorrect:**25```vue26<template>27<!-- BAD: Modifies state on every render -->28<p>{{ incrementAndGet() }}</p>2930<!-- BAD: API call on every render -->31<div>{{ fetchUserName() }}</div>3233<!-- BAD: Logging side effect -->34<span>{{ logAndFormat(date) }}</span>3536<!-- BAD: Expensive computation without caching -->37<ul>38<li v-for="item in filterAndSort(items)" :key="item.id">39{{ item.name }}40</li>41</ul>4243<!-- BAD: Random values change on every render -->44<p>{{ getRandomGreeting() }}</p>45</template>4647<script setup>48import { ref } from 'vue'4950const count = ref(0)51const items = ref([/* large array */])5253// BAD: Has side effect - modifies state54function incrementAndGet() {55count.value++ // Side effect!56return count.value57}5859// BAD: Async operation in template60async function fetchUserName() {61const res = await fetch('/api/user') // Side effect!62return (await res.json()).name63}6465// BAD: Logging is a side effect66function logAndFormat(date) {67console.log('Formatting date:', date) // Side effect!68return new Date(date).toLocaleDateString()69}7071// BAD: Expensive, runs every render without caching72function filterAndSort(items) {73return items74.filter(i => i.active)75.sort((a, b) => a.name.localeCompare(b.name))76}7778// BAD: Non-deterministic79function getRandomGreeting() {80const greetings = ['Hello', 'Hi', 'Hey']81return greetings[Math.floor(Math.random() * greetings.length)]82}83</script>84```8586**Correct:**87```vue88<template>89<!-- OK: Pure formatting function -->90<p>Count: {{ count }}</p>91<button @click="increment">Increment</button>9293<!-- OK: Data fetched via lifecycle/watcher -->94<div>{{ userName }}</div>9596<!-- OK: Pure function, no side effects -->97<span>{{ formatDate(date) }}</span>9899<!-- OK: Computed property caches result -->100<ul>101<li v-for="item in filteredAndSortedItems" :key="item.id">102{{ item.name }}103</li>104</ul>105106<!-- OK: Random value set once -->107<p>{{ greeting }}</p>108</template>109110<script setup>111import { ref, computed, onMounted } from 'vue'112113const count = ref(0)114const userName = ref('')115const date = ref(new Date())116const items = ref([/* large array */])117118// Side effects in event handlers119function increment() {120count.value++121}122123// Fetch data in lifecycle hook124onMounted(async () => {125const res = await fetch('/api/user')126userName.value = (await res.json()).name127})128129// Pure function - same input, same output130function formatDate(date) {131return new Date(date).toLocaleDateString()132}133134// Computed property - cached, only recalculates when dependencies change135const filteredAndSortedItems = computed(() => {136return items.value137.filter(i => i.active)138.sort((a, b) => a.name.localeCompare(b.name))139})140141// Set random value once, not on every render142const greetings = ['Hello', 'Hi', 'Hey']143const greeting = ref(greetings[Math.floor(Math.random() * greetings.length)])144</script>145```146147## Pure Function Guidelines148149A pure function:1501. Given the same inputs, always returns the same output1512. Does not modify any external state1523. Does not perform I/O operations (network, console, file system)1534. Does not depend on mutable external state154155```javascript156// PURE - safe for templates157function formatCurrency(amount, currency = 'USD') {158return new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(amount)159}160161function fullName(first, last) {162return `${first} ${last}`163}164165function isExpired(date) {166return new Date(date) < new Date()167}168169// IMPURE - unsafe for templates170function logAndReturn(value) {171console.log(value) // I/O172return value173}174175function getFromLocalStorage(key) {176return localStorage.getItem(key) // External state177}178179function updateAndReturn(obj, key, value) {180obj[key] = value // Mutation181return obj182}183```184185## Reference186- [Vue.js Template Syntax - Calling Functions](https://vuejs.org/guide/essentials/template-syntax.html#calling-functions)187- [Vue.js Computed Properties](https://vuejs.org/guide/essentials/computed.html)188