npm.io
0.0.3 • Published 3 years ago

react-events-hook

Licence
ISC
Version
0.0.3
Deps
1
Size
5 kB
Vulns
0
Weekly
0

React Events

Emit and listen to events in a React application.

Install

npm i react-events-hook

Usage

Configuration
import { Event } from 'react-events-hook';

const ALERT = new Event<{ message: string }>();
Emitting
ALERT.emit({ message: 'Big uff' });
Listening
const cleanup = ALERT.on((data) => {
  // ...
});

// Use cleanup function on component unmount
cleanup();
useEvent()
import React from 'react';

import { useEvent } from 'react-events-hook';

export const App: React.FC = (props) => {

  useEvent(ALERT, (data) => {
    // ...
  });

  return (
    <div>
      <h1>Hello World</h1>
    </div>
  )
}

Keywords