Skip to content
On this page

createEventHook

Category
Last Changed
9 months ago

Utility for creating event hooks

Usage

Creating a function that uses createEventHook

import { createEventHook } from '@vueuse/core'

export function useMyFetch(url) {
  const fetchResult = createEventHook<Response>()
  const fetchError = createEventHook<any>()

  fetch(url)
    .then(result => fetchResult.trigger(result))
    .catch(error => fetchError.trigger(error.message))

  return {
    onResult: fetchResult.on,
    onError: fetchError.on
  }
}
import { createEventHook } from '@vueuse/core'

export function useMyFetch(url) {
  const fetchResult = createEventHook<Response>()
  const fetchError = createEventHook<any>()

  fetch(url)
    .then(result => fetchResult.trigger(result))
    .catch(error => fetchError.trigger(error.message))

  return {
    onResult: fetchResult.on,
    onError: fetchError.on
  }
}

Using a function that uses createEventHook

<script setup lang="ts">
import { useMyFetch } from './my-fetch-function'

const { onResult, onError } = useMyFetch('my api url')

onResult((result) => {
  console.log(result)
})

onError((error) => {
  console.errr(error)
})
</script>
<script setup lang="ts">
import { useMyFetch } from './my-fetch-function'

const { onResult, onError } = useMyFetch('my api url')

onResult((result) => {
  console.log(result)
})

onError((error) => {
  console.errr(error)
})
</script>

Type Declarations

/**
 * The source code for this function was inspired by vue-apollo's `useEventHook` util
 * https://github.com/vuejs/vue-apollo/blob/v4/packages/vue-apollo-composable/src/util/useEventHook.ts
 */
export declare type EventHookOn<T = any> = (fn: (param: T) => void) => {
  off: () => void
}
export declare type EventHookOff<T = any> = (fn: (param: T) => void) => void
export declare type EventHookTrigger<T = any> = (param: T) => void
export interface EventHook<T = any> {
  on: EventHookOn<T>
  off: EventHookOff<T>
  trigger: EventHookTrigger<T>
}
/**
 * Utility for creating event hooks
 *
 * @see https://vueuse.org/createEventHook
 */
export declare function createEventHook<T = any>(): EventHook<T>
/**
 * The source code for this function was inspired by vue-apollo's `useEventHook` util
 * https://github.com/vuejs/vue-apollo/blob/v4/packages/vue-apollo-composable/src/util/useEventHook.ts
 */
export declare type EventHookOn<T = any> = (fn: (param: T) => void) => {
  off: () => void
}
export declare type EventHookOff<T = any> = (fn: (param: T) => void) => void
export declare type EventHookTrigger<T = any> = (param: T) => void
export interface EventHook<T = any> {
  on: EventHookOn<T>
  off: EventHookOff<T>
  trigger: EventHookTrigger<T>
}
/**
 * Utility for creating event hooks
 *
 * @see https://vueuse.org/createEventHook
 */
export declare function createEventHook<T = any>(): EventHook<T>

Source

SourceDocs

Contributors

Anthony Fu
wheat

Changelog

v4.11.1 on 5/24/2021
5b761 - feat: added interface (#531)
v4.10.0 on 5/12/2021
5820b - feat: new function (#497)
v4.11.1 on 5/24/2021
7b30a - feat: added interface (#531)
v4.10.0 on 5/12/2021
37cb4 - feat: new function (#497)
createEventHook has loaded