@busly/react
React hooks for the Busly event bus (
@busly/core).
This page is the API reference. For a guided introduction, see the React guide.
Install
pnpm add @busly/react @busly/coreThe peer dependencies are @busly/core, installed once per page, and react 18 or newer. The package is built on core alone and is marked 'use client'.
useBusDispatch
function useBusDispatch<G extends IBusEventGroup>(events: G, options?: EventOptions): SelfDispatchingEvents<G>;Returns one function per creator in the group. Calling one of them creates the event and dispatches it.
The result is memoized on the group and on the resolved source, so it stays stable across renders and is safe to use in dependency arrays and memoized props. Its methods are plain closures, which makes them safe to destructure.
The source comes from options.source if given, otherwise from the nearest BusProvider, and otherwise defaults to 'busly-react'.
If a payload can't be cloned, the call throws a DataCloneError right away. On the server, whether in a render body or a server action, the functions do nothing.
Keep your groups at module level. A group written inline is a new object on every render, and so it produces a new dispatcher every time.
useBusEvent
function useBusEvent<C extends IBusEventCreator>(events: C | readonly C[], handler: EventHandler<C>): void;Listens to one or more event types while the component is mounted.
The hook subscribes in a useEffect and unsubscribes on unmount, and it handles StrictMode's double mount safely. It keeps one core subscription per type and only resubscribes when the set of types changes, so passing a new handler or a new array on each render costs nothing. It always calls the latest handler.
Listing the same type twice still delivers each event once, while an empty array throws an error. If a handler throws, the others still run, because core calls each one in its own microtask. On the server, nothing is registered, since effects don't run there.
The hook deliberately has no on/lazy split. Because setState is asynchronous, ordering handlers couldn't make new state visible to later ones. One single event handler explains the alternative.
BusProvider
function BusProvider(props: BusProviderProps): JSX.Element;
interface BusProviderProps extends EventOptions {
children?: ReactNode;
}An optional provider that sets the default source for every useBusDispatch below it. Its value is memoized on source, so consumers only re-render when the source actually changes. Without a provider, the source is 'busly-react'.
<BusProvider source="checkout-mfe">
<App />
</BusProvider>Received event
type ReceivedEvent<C extends IBusEventCreator> = Envelope<ReturnType<C>['payload']> & ReturnType<C>;
type EventHandler<C extends IBusEventCreator> = (event: ReceivedEvent<C>) => void;A received event has the properties type, payload, source, ts, and v, described in What a receiver gets.
Public API
| Export | Kind | Role |
|---|---|---|
useBusDispatch | hook | Self-dispatching functions for an event group |
useBusEvent | hook | Listen to event types while mounted |
BusProvider | component | Default source for its subtree |
BusProviderProps | type | Provider props |
EventHandler | type | Handler for a creator |
ReceivedEvent | type | Event a handler receives |
Related packages
@busly/core: the bus@busly/events: event creators@busly/zustand: Zustand reducers@busly/js: the vanilla adapter, withon/lazy