0.0.3 • Published 7 months ago

use-event-bus-hook v0.0.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
7 months ago

useEventBusHook

This hook enables the use of the event bus pattern in React with zero external dependencies and ensures automatic event listener removal during cleanup, preventing memory leaks.

How to use it:

  1. First, install the package.

  npm install use-event-bus-hook
            OR
  yarn add use-event-bus-hook
  1. Add the following code into the component from which you intend to emit the event.
    import { EventBus } from "use-event-bus-hook";

    const onClick = () => {
        EventBus.emit("EVENT_NAME", 5);
    }
  1. Add the following code into the component where you intend to listen for the event.
    import { useEventBus } from "use-event-bus-hook";

    const handler = (e) => console.log(e.details); // 5
    useEventBus("EVENT_NAME", handler);
Note: Ensures that the event name should be same in both the components.

Options:

  1. eventBus
PropertyDescriptionType
onListen to eventfunction(event: string, callback)
onceListen to event only oncefunction(event: string, callback)
emitSend the datafunction(event: string, data)
offRemove the listenerfunction(event: string, callback)
  1. useEventBus
PropertyDescriptionType
nameunique event namestring
handlerTrigger when event is capturedfunction(event)

License

MIT © pulkitchdha

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago