question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useEventBus payload

See original GitHub issue
const modalBus = useEventBus<'show' | 'hide'>('modal')

modaBus.emit('show', {name: 'login-modal')

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Ideredcommented, Feb 16, 2022

mitt package api is really good. Being able to define payload type for each event type is easy

type Events = {
  foo: string;
  bar?: number;
};

const emitter = mitt<Events>(); // inferred as Emitter<Events>

emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
emitter.on('bar', (e) => {}); // 'e' has inferred type number | undefined

emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
0reactions
webfansplzcommented, Feb 16, 2022

We usually use the Mitt with Provide/Inject API . (mitt function only triggered once time)

You can use mitt directly like this :

import { inject, App, InjectionKey } from 'vue'
import mitt, { Emitter } from 'mitt'

const MittKey: InjectionKey<Emitter> = Symbol()

export function initMitt(app: App<Element>) {
    const emitter = mitt()
    app.provide(MittKey, emitter)
}

export function useEmitter() {
    return inject(MittKey)
}

But we usually use composable API in in anywhere and call it multi times , I guess that’s what useEventBus was designed for.

https://github.com/vueuse/vueuse/blob/aef8a2b2459b064ea4d92c843db2f88ef3c0eb2c/packages/core/useEventBus/internal.ts#L4

// a.vue
 const { on, emit } = useEventBus<'inc'|'dec', number>('counter')
 const { on, emit } = useEventBus<'show'|'hide', {name:string}>('modal')
// b.vue
 const { on, emit } = useEventBus<'inc'|'dec', number>('counter')
 const { on, emit } = useEventBus<'show'|'hide', {name:string}>('modal')
Read more comments on GitHub >

github_iconTop Results From Across the Web

useEventBus - VueUse
Type Declarations # ; export declare type EventBusListener<T = unknown, P = any> = ( ; event: T ·, ; payload?: P ;...
Read more >
fabienjuif/use-bus: React hook to subscribe and dispatch ...
dispatch({ type: 'string', payload: 3 }) : will dispatch the given action. useBus. import useBus from 'use-bus' : useBus(filter, callback, deps) ...
Read more >
Using event bus in Vue.js to pass data between components
This post is a guide for passing data from one component to another using a special Vue instance. September 18, 2019 4 min...
Read more >
Using the event bus - Quarkus
1, Inject the Event bus. 2, Send a message to the address greeting . Message payload is name. The EventBus object provides methods...
Read more >
How to Use Event Bus in React Architecture | by Daw-Chih Liou
'on-event-1': (payload: { data: string }) => void }const myBus = eventbus<MyBus>(). You should be able to see a clear type definition when ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found