1.0.2 • Published 3 years ago

@byteclaw/use-event-emitter v1.0.2

Weekly downloads
350
License
MIT
Repository
github
Last release
3 years ago

@Byteclaw/use-event-emitter

React hooks and components for construction of simple event emitters.

Installation

npm install @byteclaw/use-event-emitter
yarn add @byteclaw/use-event-emitter

Usage

Here is a quick example how to use these hooks.

import {
  EventEmitterContext,
  useEventEmitterInstance,
  useEventEmitter,
} from '@byteclaw/use-event-emitter';
import React, { useEffect } from 'react';

function Comp() {
  // returns the event emitter from the context
  const eventEmitter = useEventEmitter();

  useEffect(() => {
    const unsubscribe = eventEmitter.on('test', (...args) => console.log(args));

    return () => unsubscribe();
  }, []);
}

function App({ children }) {
  // returns the new instance of event emitter
  const eventEmitter = useEventEmitterInstance();

  return (
    <EventEmitterContext.Provider value={eventEmitter}>
      <Comp1 />
    </EventEmitterContext.Provider>
  );
}