Skip to content

createEventHook

Category
Export Size
247 B
Last Changed
last month

Utility for creating event hooks

Usage

Creating a function that uses createEventHook

ts
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
,
} }
js
import { createEventHook } from '@vueuse/core'
export function useMyFetch(url) {
  const fetchResult = createEventHook()
  const fetchError = createEventHook()
  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

vue
<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
.
error
(
error
)
}) </script>

Type Declarations

Show Type Declarations
ts
/**
 * 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
 */
type 
Callback
<
T
> =
IsAny
<
T
> extends true
? (...
param
: any) => void
: [
T
] extends [void]
? (...
param
: unknown[]) => void
: [
T
] extends [any[]]
? (...
param
:
T
) => void
: (...
param
: [
T
, ...unknown[]]) => void
export type
EventHookOn
<
T
= any> = (
fn
:
Callback
<
T
>) => {
off
: () => void
} export type
EventHookOff
<
T
= any> = (
fn
:
Callback
<
T
>) => void
export type
EventHookTrigger
<
T
= any> = (
...
param
:
Parameters
<
Callback
<
T
>>
) =>
Promise
<unknown[]>
export interface
EventHook
<
T
= any> {
on
:
EventHookOn
<
T
>
off
:
EventHookOff
<
T
>
trigger
:
EventHookTrigger
<
T
>
clear
: () => void
} export type
EventHookReturn
<
T
> =
EventHook
<
T
>
/** * Utility for creating event hooks * * @see https://vueuse.org/createEventHook * * @__NO_SIDE_EFFECTS__ */ export declare function
createEventHook
<
T
= any>():
EventHookReturn
<
T
>

Source

SourceDocs

Contributors

Anthony Fu
wheat
SerKo
Robin
Anthony Fu
Isi Mehmeti
OrbisK
IlyaL
Chris
Raman Paulau
Doctorwu
Curt Grimes
Guillaume Chau
丶远方
Amirreza Zarkesh
超杰
sun0day

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v13.1.0 on
c1d6e - feat(shared): ensure return types exists (#4659)
v12.6.0 on
636b8 - fix: type check for multiple arguments (#4555)
v12.1.0 on
40265 - feat: add clear function (#4378)
v10.7.0 on
3733b - fix: trigger should not ignore falsy values (#3561)
e48ca - fix: make createEventHook union type can be inferred correctly (#3569)
v10.6.0 on
6040e - feat: allow trigger to optionally have no parameters (#3507)

Released under the MIT License.

Join the Biggest FREE AI-Driven Development Event for Vue Developers
Save My Seat