react-mitt
React event emitter / pubsub
A react wrapper for the awesome mitt library
Live demo https://codesandbox.io/s/react-mitt-demo-n11ii
Install
$Ā npmĀ installĀ react-mitt
Usage
Wrap your app in the MittProvider
component, providing descendants access to the useMitt
hook for event pubsub
importĀ Ā MittProviderĀ Ā fromĀ "react-mitt"Ā Ā {Ā Ā returnĀ Ā Ā Ā Ā <MittProvider>Ā Ā Ā Ā Ā Ā <ComponentAĀ />Ā Ā Ā Ā Ā Ā <ComponentBĀ />Ā Ā Ā Ā </MittProvider>Ā Ā }
Example
Once you wrap your app with the MittProvider
as demonstrated above, event pubsub becomes a breeze with the useMitt
hook
importĀ Ā useMittĀ Ā fromĀ 'react-mitt'Ā Ā {Ā Ā constĀ Ā emitterĀ Ā =Ā Ā Ā Ā constĀ handleClickĀ =Ā Ā Ā Ā Ā Ā Ā emitterĀ Ā Ā Ā Ā returnĀ <buttonĀ =>emit!</button>}Ā Ā {Ā Ā constĀ Ā emitterĀ Ā =Ā Ā Ā Ā Ā Ā Ā returnĀ ...}Ā
Hook
The useMitt
hook has the following signature
constĀ Ā emitterĀ Ā =Ā
For usage of the emitter object, see mitt API docs