Skip to content

Commit

Permalink
fix(useEventListener): immutable options on removal, close #2825 (#3346)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Aug 28, 2023
1 parent 0e4a5f0 commit 3ef59cb
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/core/useEventListener/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Arrayable, Fn, MaybeRefOrGetter } from '@vueuse/shared'
import { noop, toValue, tryOnScopeDispose } from '@vueuse/shared'
import { isObject, noop, toValue, tryOnScopeDispose } from '@vueuse/shared'
import { watch } from 'vue-demi'
import type { MaybeElementRef } from '../unrefElement'
import { unrefElement } from '../unrefElement'
Expand Down Expand Up @@ -163,9 +163,11 @@ export function useEventListener(...args: any[]) {
if (!el)
return

// create a clone of options, to avoid it being changed reactively on removal
const optionsClone = isObject(options) ? { ...options } : options
cleanups.push(
...(events as string[]).flatMap((event) => {
return (listeners as Function[]).map(listener => register(el, event, listener, options))
return (listeners as Function[]).map(listener => register(el, event, listener, optionsClone))
}),
)
},
Expand Down

0 comments on commit 3ef59cb

Please sign in to comment.