React Global Events
Broadcast SyntheticEvents
from any React component to any others which are subscribed to that event.
Installation
npm install --save react-global-events
Usage
The most common implementation is to have a single root component listen for events.
// RootComponent.js const Root = <div id="app-root" ...> ... <MySubComponent /> </div>
Subcomponents may then subscribe to those events in componentDidMount
. They must then implement callback methods with the naming convention onGlobal<ReactEventName>
. Note that the <ReactEventName>
follows the camel-casing convetions of event names in React, not standard JS: onGlobalMouseDown
rather than onGlobalMousedown
.
// MySubComponent.js const MySubComponent = React
You can have multiple components listenFor
events, but they can't choose which subscribers to broadcast to. The events are still broadcast globally.