1.0.1 • Published 1 year ago

event-bus-react v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

React Event Bus

Old Usages ( < v 1.0.0 )

import useEventBus from "event-bus-react";

const App = () => {
  const { subscribe } = useEventBus("MyEvent"); // default EventBus

  subscribe("alert", (data) => {
    console.log(data);
  });

  const handleEmitEvent = () => {
    MyEvent.emit("alert", { sayHello: "hello" });
  };

  return (
    <>
      <button onClick={handleEmitEvent}>Emit Event</button>
    </>
  );
};

New Usages ( >= v1.0.0 )

1.) Install event-bus-react package from npm.js

npm i event-bus-react

2.) Declare useEventBus() in the entry file of react or next.js project.

import useEventBus from "event-bus-react";

useEventBus();

3.) Subscribe the event anywhere in the component or pages.

import { subscribe } from "event-bus-react";

subscribe(
  "show-alert",
  () => {
    // do something...
  },
  []
);

4.) Emit the event wherever you are in the component tree.

EventBus.emit("show-alert");

Api Declaration

useEventBus(name)

useEventBus(); // EventBus
useEventBus("MyCustomEvent");

subscribe(eventName, callBack, dependencies)

subscribe("show-alert", (data) => {
  alert(data?.message);
});

subscribe(
  "show-result",
  () => {
    console.log(a + b);
  },
  [a, b]
);

emit(eventName, data)

EventBus.emit("show-alert", { message: "hello" });

MyCustomEvent.emit("show-result");
1.0.1

1 year ago

1.0.0

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

2.0.2

2 years ago