@ouroboros/events
v1.1.0
Published
Package to give the ability to subscribe to and trigger synchronous events in javascript. Useful for passing data around a project without creating import conflicts.
Downloads
86
Maintainers
Readme
@ouroboros/events
A library to give the ability to subscribe to and trigger synchronous events in javascript. Useful for passing data around a project without creating import conflicts / circular dependencies.
Installation
npm
npm install @ouroboros/events
Getting Started
Import events into your code
import events from '@ouroboros/events';
Subscribing and unsubscribing in a React useEffect hook:
export default function App() {
useEffect(() => {
const headerClick = (element) => {alert(`Header ${element} element was clicked!`)}
events.subscribe('header', headerClick);
return () => {
events.unsubscribe('header', headerClick');
}
}, []);
return (
<Header />
)
}
Triggering an event from another component:
export default function Header(props) {
return (
<div onClick={() => {
events.trigger('header', 'div');
}}>
<p onClick={() => {
events.trigger('header', 'p');
}}>Header Content</p>
</div>
);
}