UNPKG

@sendbird/uikit-chat-hooks

Version:

A set of React hooks for integrating Sendbird chat functionality into your React app.

24 lines 1.05 kB
import { useEffect, useLayoutEffect, useRef } from 'react'; import { UserEventHandler } from '@sendbird/chat'; import { Logger } from '@sendbird/uikit-utils'; export const useUserEventHandler = (sdk, handlerId, hookHandler) => { const handlerRef = useRef(hookHandler); useLayoutEffect(() => { handlerRef.current = hookHandler; }); useEffect(() => { Logger.debug('[useUserEventHandler] hook called by', handlerId); const handler = new UserEventHandler(); const handlerKeys = Object.keys(handler); handlerKeys.forEach(key => { handler[key] = (...args) => { var _handlerRef$current$k, _handlerRef$current; // @ts-ignore (_handlerRef$current$k = (_handlerRef$current = handlerRef.current)[key]) === null || _handlerRef$current$k === void 0 || _handlerRef$current$k.call(_handlerRef$current, ...args); }; }); sdk.addUserEventHandler(handlerId, handler); return () => sdk.removeUserEventHandler(handlerId); }, [sdk, handlerId]); }; //# sourceMappingURL=useUserEventHandler.js.map