@byteclaw/use-event-emitter
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i @byteclaw/use-event-emitter

Weekly Downloads

284

Version

1.0.2

License

MIT

Unpacked Size

15.7 kB

Total Files

13

Last publish

Collaborators

  • jurajhrib
  • michalkvasnicak