React hooks for integrating with the @psionic/emit library.
Methods
# static useOnEmit(eventName, callback) → {Array.<StartListenerCallback, CancelListenerCallback, ListenerIsActive>}
React Hook that creates a new listener for a given event when the component mounts, and cancels the listener whenever the component unmounts.
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string
|
The name of the event to create the listener for |
callback |
function
|
The function to call whenever the given event is emitted; this function should take in the additional data object as its only param |
Hook API for interacting with the listener created via the hook
Examples
// Use a "saved" event listener for the lifetime of the component
useOnEmit('saved', () => console.log('Something was saved!'));
// Create the "saved" event listener for the lifetime of the component
const [startSavedListener, cancelSavedListener, savedListenerIsActive] = useOnEmit('saved', () => console.log('Something was saved!'));
// Cancel the "saved" event listener early
cancelSavedListener();
// Start the "saved" event listener back up
startSavedListener();
Type Definitions
# CancelListenerCallback()
Callback to cancel the listener created via useOnEmit
if it currently is listening. This function has no effect if
the listener is not actively listening.
boolean
# ListenerIsActive
Boolean state value tracking whether the listener is currently active or not.
# StartListenerCallback()
Callback to start the listener created via useOnEmit
if it hasn't already been started, or if it had been canceled.
This function has no effect if the listener has already been started.