use Socket hook

PHOTO EMBED

Wed Oct 28 2020 18:02:16 GMT+0000 (Coordinated Universal Time)

Saved by @Tony Dung #javascript

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,
  };
};
content_copyCOPY

Call this hook only one time in the Main screen