@mozart25/react-event-bus
v1.0.3
Published
A simple event bus for React applications
Downloads
9
Maintainers
Readme
@mozart25/react-event-bus
A simple event bus for React and Next.js applications.
Installation
npm install @mozart25/react-event-bus
Usage
In ComponentA
import React from "react";
import eventBus from "@mozart25/react-event-bus";
const ComponentA = () => {
const handleClick = () => {
eventBus.emit("customEvent", "Hello from ComponentA");
};
return <button onClick={handleClick}>Send Event</button>;
};
export default ComponentA;
In ComponentB
import React, { useEffect, useState } from "react";
import eventBus from "@mozart25/react-event-bus";
const ComponentB = () => {
const [message, setMessage] = useState("");
useEffect(() => {
const handleEvent = (data) => {
setMessage(data);
};
eventBus.on("customEvent", handleEvent);
return () => {
eventBus.off("customEvent", handleEvent);
};
}, []);
return <div>Received Message: {message}</div>;
};
export default ComponentB;
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or improvements.
License
This project is licensed under the MIT License.