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

2.0.3 • Public • Published

Events

This package enables React components to communicate without direct coupling, by providing a dispatch/listen concept. It allows components to dispatch events that can be listened to by other components, facilitating communication and coordination. Developers can use it to create a modular and scalable architecture, as components can be added, removed, or modified without affecting the rest of the application.

Installation

You can install the package via pnpm:

pnpm install @thirty-one/events

Usage

Here's an example of how to use the package:

import { useDispatch, useListen } from "@thirty-one/events";

const COUNTER_CHANGE = "counter:change";

function Listener() {
	const [count, setCount] = useState(0);

	useListen(COUNTER_CHANGE, () => {
		setCount((c) => c + 1);
	});

	return <div>Count: {count}</div>;
}

function Dispatcher() {
	const dispatch = useDispatch();

	return (
		<button type="button" onClick={() => dispatch(COUNTER_CHANGE)}>
			Increase
		</button>
	);
}

function App() {
	return (
		<>
			<Listener />
			<Dispatcher />
		</>
	);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @thirty-one/events

Weekly Downloads

2

Version

2.0.3

License

none

Unpacked Size

9.97 kB

Total Files

7

Last publish

Collaborators

  • rui.duarte