Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 814 Bytes

File metadata and controls

46 lines (34 loc) · 814 Bytes
category
Utilities

createEventHook

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
  }
}

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>