UNPKG

@sendbird/uikit-chat-hooks

Version:

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

24 lines 1.03 kB
import { useEffect, useLayoutEffect, useRef } from 'react'; import { ConnectionHandler } from '@sendbird/chat'; import { Logger } from '@sendbird/uikit-utils'; export const useConnectionHandler = (sdk, handlerId, hookHandler) => { const handlerRef = useRef(); useLayoutEffect(() => { handlerRef.current = hookHandler; }); useEffect(() => { Logger.info('[useConnectionHandler]', handlerId); const handler = new ConnectionHandler(); 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.addConnectionHandler(handlerId, handler); return () => sdk.removeConnectionHandler(handlerId); }, [sdk, handlerId]); }; //# sourceMappingURL=useConnectionHandler.js.map