Skip to content

Commit

Permalink
use onBeforeUnmount in useEventListener
Browse files Browse the repository at this point in the history
use `onBeforeUnmount` in `useEventListener` instead of `onUnmounted` since `onUnmounted will not work if you trying to pass some element ref from the consumer component.
Using the  `onUnmounted` the ref will be null since the component is unmounted, so we should use `onBeforeUnmount`
  • Loading branch information
bnabriss committed May 5, 2023
1 parent efe4dc8 commit 0d74d7e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/guide/reusability/composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ For example, we can extract the logic of adding and removing a DOM event listene

```js
// event.js
import { onMounted, onUnmounted } from 'vue'
import { onMounted, onBeforeUnmount } from 'vue'

export function useEventListener(target, event, callback) {
// if you want, you can also make this
// support selector strings as target
onMounted(() => target.addEventListener(event, callback))
onUnmounted(() => target.removeEventListener(event, callback))
onBeforeUnmount(() => target.removeEventListener(event, callback))
}
```

Expand Down

0 comments on commit 0d74d7e

Please sign in to comment.