@ouroboros/events
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@ouroboros/events

npm version MIT License

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

Package Sidebar

Install

npm i @ouroboros/events

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

12.8 kB

Total Files

12

Last publish

Collaborators

  • ouroboros