Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/createEventHook.md
1---2category: Utilities3---45# createEventHook67Utility for creating event hooks89## Usage1011Creating a function that uses `createEventHook`1213```ts14import { createEventHook } from '@vueuse/core'1516export function useMyFetch(url) {17const fetchResult = createEventHook<Response>()18const fetchError = createEventHook<any>()1920fetch(url)21.then(result => fetchResult.trigger(result))22.catch(error => fetchError.trigger(error.message))2324return {25onResult: fetchResult.on,26onError: fetchError.on,27}28}29```3031Using a function that uses `createEventHook`3233```vue34<script setup lang="ts">35import { useMyFetch } from './my-fetch-function'3637const { onResult, onError } = useMyFetch('my api url')3839onResult((result) => {40console.log(result)41})4243onError((error) => {44console.error(error)45})46</script>47```4849## Type Declarations5051```ts52/**53* The source code for this function was inspired by vue-apollo's `useEventHook` util54* https://github.com/vuejs/vue-apollo/blob/v4/packages/vue-apollo-composable/src/util/useEventHook.ts55*/56type Callback<T> =57IsAny<T> extends true58? (...param: any) => void59: [T] extends [void]60? (...param: unknown[]) => void61: [T] extends [any[]]62? (...param: T) => void63: (...param: [T, ...unknown[]]) => void64export type EventHookOn<T = any> = (fn: Callback<T>) => {65off: () => void66}67export type EventHookOff<T = any> = (fn: Callback<T>) => void68export type EventHookTrigger<T = any> = (69...param: Parameters<Callback<T>>70) => Promise<unknown[]>71export interface EventHook<T = any> {72on: EventHookOn<T>73off: EventHookOff<T>74trigger: EventHookTrigger<T>75clear: () => void76}77export type EventHookReturn<T> = EventHook<T>78/**79* Utility for creating event hooks80*81* @see https://vueuse.org/createEventHook82*83* @__NO_SIDE_EFFECTS__84*/85export declare function createEventHook<T = any>(): EventHookReturn<T>86```87