Skip to content

Latest commit

 

History

History

use-event-emitter

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

@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>
  );
}