Gene documentation
Components
Hooks API
useMediator API

useMediator API

The useMediator hook is implemented using useEffect and the native addEventListener function. Events handled by useMediator will not propagate further, as mediation is the final stage in the event handling process.

Arguments

The useMediator hook accepts three arguments:

useMediator(eventName, eventHandler, HTMLElement)

where:

ArgumentDescription
eventNameThe name of the event to listen for.
eventHandlerA handler function that processes the event payload.
Example: function handleMyEvent(payload) { console.log(payload) }
HTMLElementThe HTML element to which the listener is attached.

Example Usage

function ReactComponent() {
  const ref = useRef()
 
  function handleMyEvent(payload) {
    // Handle the event payload, e.g., by navigating or fetching data
    console.log(payload)
  }
 
  useMediator('myEvent', handleMyEvent, ref)
 
  return <div ref={ref}>...</div>
}