@aux4/use-handler

1.0.5 • Public • Published

use-handler

React hook to handle events dispatched by @aux4/dispatcher.js.

Install

npm install @aux4/use-handler

Usage

The useHandler hook registers the specified events to the Dispatcher.js, and unregister all the events for the subscriber when the react component is unmounted.

Using event type and value

Dispatcher.dispatch(Events.EVENT_ONE, "ok");
...
useHandler((eventType, value) => {
  /*
   * eventType = Events.EVENT_ONE
   * value = "ok"
   */
  ...
}, Events.EVENT_ONE);

Using event type only

Dispatcher.dispatch(Events.EVENT_ONE);
...
useHandler((eventType) => {
  /*
   * eventType = Events.EVENT_ONE
   */
  ...
}, Events.EVENT_ONE);

Using event only

Dispatcher.dispatch(Events.EVENT_ONE);
...
useHandler(() => {
  ...
}, Events.EVENT_ONE);

Using multiple events

Dispatcher.dispatch(Events.EVENT_ONE, "one");
Dispatcher.dispatch(Events.EVENT_TWO, "two");
...
useHandler((eventType, value) => {
  if (eventType === Events.EVENT_ONE) {
      /*
       * eventType = Events.EVENT_ONE
       * value = "one"
       */
    ...
  } else if (eventType === Events.EVENT_TWO) {
    /*
     * eventType = Events.EVENT_TWO
     * value = "two"
     */
    ...  
  }
  ...
}, [Events.EVENT_ONE, Events.EVENT_TWO]);

Dependents (0)

Package Sidebar

Install

npm i @aux4/use-handler

Weekly Downloads

0

Version

1.0.5

License

Apache-2.0

Unpacked Size

2.47 kB

Total Files

3

Last publish

Collaborators

  • aux4-dev