0.0.3 • Published 2 years ago
react-events-hook v0.0.3
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>
)
}