Mitt is a JavaScript event management library, based on a publish/subscribe pattern that stands out for its simplicity and small size.
Created by Jason Miller, Mitt is a minimalistic and extremely lightweight library. It weighs less than 200 bytes (minified and gzipped).
Mitt has no dependencies, and is particularly useful in small to medium applications where the complexity of larger and heavier libraries is not required.
Main Features
- Lightweight: Mitt is extremely small
- Simple and easy to use: Its API is very easy to understand and use,
- No dependencies: Mitt does not rely on other libraries
With Mitt, it is very simple to implement a pattern like an Event Bus (a shared mechanism for components to share messages):
It is a very useful library in many situations, such as for communication between React, Vue, Svelte components, etc.
Besides leading to a bunch of mess in your code, which will get you out of a bind, and drive architecture purists crazy (which is always a fun plus 😅)
Basic Usage of Mitt
To start using Mitt, you first need to install it in your project.
npm install mitt
You can also include Mitt directly in your project via a CDN:
<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>
Once you have installed Mitt, you can start using it in your project. Mitt works by creating an “event bus” that allows you to register and listen to custom events.
First, you need to create an instance of Mitt. This is done by calling the mitt()
function.
import mitt from 'mitt';
const emitter = mitt();
To listen to an event, we use the on
method. This method takes two arguments: the event name and the function that will be executed when the event occurs.
emitter.on('greeting', (message) => {
console.log(message); // "Hello, world!"
});
To emit an event, we use the emit
method. This method takes two arguments: the event name and the data you want to pass to the listeners.
emitter.emit('greeting', 'Hello, world!');
If at any point you no longer need to listen to an event, you can remove the listener using the off
method.
Assuming you created an event emitter.on('greeting', handler);
// Later, when you no longer need the listener
emitter.off('greeting', handler);
If you want to remove all listeners for a particular event, you can use the off
method without passing the handler function.
emitter.off('greeting');
Practical Example
Let’s see how to put everything together to see how the use of Mitt would work.
import mitt from 'mitt';
// Create an event bus
const emitter = mitt();
// Listen to an event
emitter.on('greeting', (message) => {
console.log('Received:', message);
});
// Emit the event
emitter.emit('greeting', 'Hello, Mitt!');
Example in a React application
I mentioned that one of the advantages of Mitt is that it allows communication between components, even across different frameworks, where the usual methods would be complicated.
Let’s see this, for example, in a React example. First, we create a global event bus with a Singleton pattern.
To do this, we create a file eventBus.js
:
import mitt from 'mitt';
const eventBus = mitt();
export default eventBus;
Now, we can emit events from any component
import eventBus from './eventBus';
const Sender = () => {
return (
<button onClick={() => eventBus.emit('message', 'Hello from Sender')}>
Send Message
</button>
);
};
export default Sender;
Now we can listen to events in another component, for example like this,
import { useEffect, useState } from 'react';
import eventBus from './eventBus';
const Receiver = () => {
const [message, setMessage] = useState('');
useEffect(() => {
const handler = (msg) => setMessage(msg);
eventBus.on('message', handler);
return () => {
eventBus.off('message', handler);
};
}, []);
return <p>Message received: {message}</p>;
};
export default Receiver;
I’ve mentioned it jokingly above, but just to clarify don’t abuse this in your code. It is just one more tool, which can be very useful, but it can also complicate your app.
Mitt is Open Source and all its code and documentation are available in the Official Mitt Documentation