Skip to content

Instantly share code, notes, and snippets.

@ruanvalente
Created November 26, 2024 17:46
Show Gist options
  • Save ruanvalente/47358e3b1d215f7d596da824861a232e to your computer and use it in GitHub Desktop.
Save ruanvalente/47358e3b1d215f7d596da824861a232e to your computer and use it in GitHub Desktop.

Revisions

  1. ruanvalente revised this gist Nov 26, 2024. No changes.
  2. ruanvalente created this gist Nov 26, 2024.
    14 changes: 14 additions & 0 deletions useEmit.tsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    type EmitFunction<T> = (eventType: string, detail: T) => void;

    const useEmit = <T,>(): EmitFunction<T> => {
    const emit: EmitFunction<T> = (eventType: string, detail: T) => {
    const customEvent = new CustomEvent(eventType, {
    detail
    })
    window.dispatchEvent(customEvent)
    }

    return emit
    }

    export default useEmit