react-event-hook
A library for emitting and listening to events in a React application.
Installation
To use react-event-hook in your project, run:
yarn add react-event-hook
Listening for events
Event listeners can be registered using the useListener
hook. Listeners are automatically removed when components are unmounted to avoid memory leaks.
import { useListener } from "react-event-hook";
const ListenerComponent = () => {
const [count, setCount] = useState(0);
useListener("increment", () => {
setCount(count + 1);
});
return <div>{count}</div>;
};
Emitting events
Events can be emitted from anywhere in your application using the useEmitter
hook.
import { useEmitter } from "react-event-hook";
const EmitterComponent = () => {
const emit = useEmitter();
return <button onClick={() => emit("increment")}>Increment</button>;
};
Contributing
When contributing to this project, please first discuss the change you wish to make via a GitHub issue before making a change.
Run yarn test
and update the tests if needed.
License
This project is licensed under the MIT License – see the LICENSE file for details.