import { useEffect, useRef } from 'react'; import { useSelector } from 'react-redux'; const useAppSocket = () => { const token = useSelector(selectAccessToken()); const user = useSelector(selectUser()); const { set: setChatAccount } = useSharedValue( AppConstants.sharedValueKey.CHAT_ACCOUNT, {}, ); const socket = useRef(null); useEffect(() => { if (user && token) { socket.current = new SocketClient({ userId: user.id, userIdentityName: user.identityName, token: token, onSocketError: () => {}, onChatInfo: setChatAccount, }); GlobalLib.SocketClient.set(socket.current); } }, [token, user, setChatAccount]); return { get: GlobalLib.SocketClient.get, socket, }; };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter