1.0.5 • Published 3 years ago

@aux4/use-handler v1.0.5

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 years ago

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]);