event-emitter.tsx 773 B

12345678910111213141516171819202122232425262728
  1. 'use client'
  2. import { createContext, useContext } from 'use-context-selector'
  3. import { useEventEmitter } from 'ahooks'
  4. import type { EventEmitter } from 'ahooks/lib/useEventEmitter'
  5. const EventEmitterContext = createContext<{ eventEmitter: EventEmitter<string> | null }>({
  6. eventEmitter: null,
  7. })
  8. export const useEventEmitterContextContext = () => useContext(EventEmitterContext)
  9. type EventEmitterContextProviderProps = {
  10. children: React.ReactNode
  11. }
  12. export const EventEmitterContextProvider = ({
  13. children,
  14. }: EventEmitterContextProviderProps) => {
  15. const eventEmitter = useEventEmitter<string>()
  16. return (
  17. <EventEmitterContext.Provider value={{ eventEmitter }}>
  18. {children}
  19. </EventEmitterContext.Provider>
  20. )
  21. }
  22. export default EventEmitterContext