A lightweight and flexible event bus for React, designed to simplify inter-component communication using event-driven architecture.
β
Easy to integrate with any React project
β
TypeScript support out of the box
β
Supports both global and scoped events
β
Handles dynamic event registration and cleanup
β
Clean API for emitting and listening to events
β
Fully compatible with React 19
Install the package using npm:
npm install react-ebus
or with yarn:
yarn add react-ebus
Wrap your app with the EventProvider
to initialize the event bus:
import React from "react";
import { EventProvider } from "react-ebus";
const App = () => {
return (
<EventProvider
registerEvents={{
userlogin: "user:login",
userlogout: "user:logout",
}}
allowAllEvents={false}
>
{/* If `allowAllEvents` is false, only registered events will be allowed */}
<YourComponent />
</EventProvider>
);
};
export default App;
Use the useEventEmitter
hook to emit events from any component.
import { useEventEmitter } from "react-ebus";
const YourComponent = () => {
const { emit, eventList } = useEventEmitter();
const handleLogin = () => {
emit("user:login", { id: 1, name: "John Doe" }); // Emit directly by event name
};
const handleLoginWithRegisteredEvent = () => {
emit(eventList.userlogin, { id: 1, name: "John Doe" }); // Emit using registered events to avoid typos
};
return (
<div>
<button onClick={handleLogin}>Login</button>
<button onClick={handleLoginWithRegisteredEvent}>
Login with Registered Event
</button>
</div>
);
};
Use the useEventListener
hook to listen for events.
import { useEventListener } from "react-ebus";
const YourComponent = () => {
const { unregister, unregisterAll } = useEventListener(
{
"user:login": (data) => {
console.log("User logged in:", data);
},
"user:logout": () => {
console.log("User logged out");
},
},
{ allowedAllEvents: false }
);
return (
<div>
<p>Listening for login and logout events...</p>
<button onClick={() => unregister("user:login")}>
Unregister Login Event
</button>
<button onClick={unregisterAll}>Unregister All Events</button>
</div>
);
};
Prop | Type | Required | Description |
---|---|---|---|
registerEvents |
{[key:string]:string} |
β | List of allowed event names. |
allowAllEvents |
boolean |
β | If false , only registered events are allowed. |
Prop | Type | Description |
---|---|---|
emit |
(eventName: string, payload?: any) => void |
Function to emit an event with an optional payload. |
eventList |
{[key: string]: string} |
List of registered events. |
isEventAllowed |
(eventName: string) => boolean |
Function to check if an event is allowed. |
Prop | Type | Required | Description |
---|---|---|---|
eventListeners |
Record<string, EventHandler> |
β | Object mapping event names to handler functions. |
configuration |
Partial<UseEventListenerConfig> |
β | Configuration object for allowing unregistered events. |
Prop | Type | Description |
---|---|---|
allowedAllEvents |
boolean |
If true , allows emitting events even if unregistered. |
import React from "react";
import {
EventProvider,
useEventEmitter,
useEventListener,
} from "react-ebus";
const App = () => (
<EventProvider
registerEvents={{ customEvent: "custom:event" }}
allowAllEvents={false}
>
<ComponentA />
<ComponentB />
</EventProvider>
);
const ComponentA = () => {
const { emit } = useEventEmitter();
return (
<button onClick={() => emit("custom:event", { message: "Hello from A!" })}>
Emit Event
</button>
);
};
const ComponentB = () => {
useEventListener({
"custom:event": (data) => {
console.log("Event received:", data);
},
});
return <div>Listening for events...</div>;
};
Types are included out of the box:
export type EventHandler = (...args: any[]) => void;
export interface UseEventListenerConfig {
allowedAllEvents?: boolean;
}
npm run type-check
npm run build
npm publish --access public
βοΈ Always define and register events in EventProvider
.
βοΈ Clean up event listeners to avoid memory leaks.
βοΈ Use TypeScript to ensure type safety.
βοΈ Handle unknown or unregistered events gracefully.
If you like this project, consider giving it a β on GitHub!
Created by Saurabh
This project is licensed under the MIT License.
β
Updated with useEventEmitter
and useEventListener
changes.
β
Improved formatting for better readability.
β
Added demo link and GitHub star button.
β
Fixed consistency across examples.