React hook to derive state from events emitted by an EventEmitter
npm install use-event-latchReact hook returning a state value derived from an event emitter. For use with EventEmitter instances from the events module for browsers (included by bundlers like browserify or webpack, see here).
Features:
- configure which events to listen to, and how to derive the returned state from the emitted event and arguments
- handles registering (and removing) listeners on the event emitter
The hook can be seen as a state machine, with the state initially being returned as null. The hook's second configuration param specifies the state transitions to take in response to triggering events from the underlying event emitter.
Useful for example in displaying the status of a client which may transition between several connection states.
``sh`
npm install use-event-latch
You can wrap any event emitter into useEventLatch.
`jsx
import { useEventLatch } from 'use-event-latch'
function StatusIndicator() {
const client = useRef(new MyClient())
const clientStatus = useEventLatch(client.current)
return (
color={clientStatus === 'connected' ? 'green' : 'red'}
content={Client status: ${clientStatus}}`
/>
)
}
With the monitored client being some event emitter:
`js
import EventEmitter from 'events'
export class MyClient extends EventEmitter {
connect() {
// ...
this.emit('connected')
}
handleClose(err) {
// ...
this.emit('disconnected', err)
}
}
`
The hook takes in a second optional parameter to specify how the state should be derived from the emitted events.
If the status was instead emitted as an argument under a single status-changed event:
`jsx
import { useEventLatch } from 'use-event-latch'
function StatusIndicator() {
const client = useRef(new MyClient())
const clientStatus = useEventLatch(
client.current,
{
'status-changed': (statusString) => {
// could return anything here, for example to transform the status string
return statusString
},
},
)
return (
color={clientStatus === 'connected' ? 'green' : 'red'}
content={Client status: ${clientStatus}}``
/>
)
}