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/undeclared-emits-double-firing.md
1---2title: Undeclared Emits Cause Double Event Firing3impact: HIGH4impactDescription: Native events re-emitted without declaration fire twice - once from emit() and once from native listener on root element5type: gotcha6tags: [vue3, emits, defineEmits, events, native-events, fallthrough]7---89# Undeclared Emits Cause Double Event Firing1011**Impact: HIGH** - When a component re-emits a native DOM event (like `click`) without declaring it in `emits`, the event can fire twice. This happens because undeclared event listeners become part of `$attrs` and fall through to the root element, where they listen for native events while your `emit()` call also fires.1213This is a common bug when wrapping native elements or migrating from Vue 2 to Vue 3.1415## Task Checklist1617- [ ] Declare all emitted events in `defineEmits()` or `emits` option18- [ ] Pay special attention when re-emitting native events (click, input, change, etc.)19- [ ] Check for double firing if component wraps native elements20- [ ] Understand that undeclared listeners fall through to `$attrs`2122## The Problem2324**Incorrect - Undeclared emit causes double firing:**25```vue26<!-- MyButton.vue -->27<script setup>28// NO defineEmits declaration!29</script>3031<template>32<!-- Native click listener from parent falls through to button -->33<!-- PLUS we re-emit click -->34<button @click="$emit('click', $event)">35<slot></slot>36</button>37</template>38```3940```vue41<!-- Parent.vue -->42<template>43<!-- This handler fires TWICE on each click! -->44<MyButton @click="handleClick">Click me</MyButton>45</template>4647<script setup>48function handleClick() {49console.log('clicked') // Logs twice!50}51</script>52```5354**What happens:**551. User clicks the button562. Native click event fires on the button element573. `@click` falls through to button (because 'click' isn't in emits), triggering `handleClick`584. The button's `@click="$emit('click', $event)"` also fires, emitting a component event595. Parent's `@click="handleClick"` receives the emitted event, triggering `handleClick` again6061## The Solution6263**Correct - Declare the emit:**64```vue65<!-- MyButton.vue -->66<script setup>67// Declare 'click' as a component event68const emit = defineEmits(['click'])69</script>7071<template>72<!-- Now @click="handleClick" in parent only listens to emit() -->73<button @click="emit('click', $event)">74<slot></slot>75</button>76</template>77```7879```vue80<!-- Parent.vue -->81<template>82<!-- Now fires only once -->83<MyButton @click="handleClick">Click me</MyButton>84</template>85```8687When you declare `click` in `emits`:88- Vue knows `@click` on the component is listening for a component event89- The listener does NOT fall through to the root element90- Only your explicit `emit('click')` triggers the parent's handler9192## Options API Version9394**Correct - Using emits option:**95```vue96<script>97export default {98emits: ['click', 'input', 'change'],99100methods: {101handleClick(event) {102this.$emit('click', event)103}104}105}106</script>107```108109## Common Scenarios110111### Wrapping Form Inputs112113```vue114<!-- CustomInput.vue -->115<script setup>116// Declare all events you re-emit117const emit = defineEmits(['input', 'change', 'focus', 'blur'])118</script>119120<template>121<input122@input="emit('input', $event)"123@change="emit('change', $event)"124@focus="emit('focus', $event)"125@blur="emit('blur', $event)"126/>127</template>128```129130### Wrapper Components131132```vue133<!-- IconButton.vue -->134<script setup>135const emit = defineEmits(['click'])136</script>137138<template>139<button @click="emit('click', $event)">140<Icon :name="icon" />141<slot></slot>142</button>143</template>144```145146## Alternative: Don't Re-emit, Let It Fall Through147148If your component has a single root element and you want native event behavior:149150```vue151<!-- MyButton.vue -->152<script setup>153// Don't declare 'click' - let it fall through naturally154const emit = defineEmits(['custom-action'])155</script>156157<template>158<!-- Native @click from parent falls through to this button -->159<button>160<slot></slot>161</button>162</template>163```164165```vue166<!-- Parent.vue -->167<template>168<!-- This native click falls through to the button -->169<MyButton @click="handleClick">Click me</MyButton>170</template>171```172173This works because:174- You don't re-emit 'click' explicitly175- The native click listener falls through to the single root button176- Native click fires once when button is clicked177178## Debugging Double Firing179180```vue181<script setup>182function handleClick(event) {183console.log('Event type:', event?.type)184console.log('Is native:', event instanceof Event)185console.trace('Click handler called')186}187</script>188```189190If you see two stack traces with different origins, you have the double-firing issue.191192## Reference193- [Vue 3 Migration - emits Option](https://v3-migration.vuejs.org/breaking-changes/emits-option)194- [Vue.js Component Events](https://vuejs.org/guide/components/events.html)195- [Vue.js Fallthrough Attributes](https://vuejs.org/guide/components/attrs.html)196