Home > other >  Not getting updated state value in socket callback react native
Not getting updated state value in socket callback react native

Time:05-21

I have listend an event in customhook and when that event works, I have to do some logic there with state.but now I only get empty array every time that event callback works.

const useChatHistoryList = () => {
  const sk = useSocket();
  const [chatList, setChatList] = useState([]);
  const [end, setEnd] = useState(true);

  useEffect(() => {
    sk.emit('chatlist');
  }, [start]);

 
  useEffect(() => {
    const onChatListReceived = data => {
      const _data = JSON.parse(data);
      setHistoryLoading(false);
      setChatList(_data);
    };

    const onChatListToUpdateReceived = data => {
      const _data = JSON.parse(data);
      console.log(chatList);//getting only empty array everytime
    };

    sk.on('chatlist', onChatListReceived);
    sk.on('chatlistToUpdate', onChatListToUpdateReceived);
    return () => {
      sk.off('chatlistToUpdate');
      sk.off('chatlist');
    };
  }, []);

  return { chatList,end};
};

CodePudding user response:

I have not used socket.io before but this is what I meant by asynchronous update. From your code, it looked to me like your callback is getting called before the state is updated. So to solve this, I added a useEffect() with chatList as a dependency so that callback gets called every time chatList gets updated. I hope this makes sense.

const useChatHistoryList = () => {
  const sk = useSocket();
  const [chatList, setChatList] = useState([]);
  const [end, setEnd] = useState(true);

  const onChatListReceived = data => {
    const _data = JSON.parse(data);
    setHistoryLoading(false);
    setChatList(_data);
  };

  const onChatListToUpdateReceived = data => {
    const _data = JSON.parse(data);
    console.log(chatList); //getting only empty array everytime
  };

  useEffect(() => {
    sk.on('chatlist', onChatListReceived);
    sk.on('chatlistToUpdate', onChatListToUpdateReceived);
    return () => {
      sk.off('chatlistToUpdate');
      sk.off('chatlist');
    };
  }, []);

  // Emit chatlistToUpdate whenever chatList is updated
  useEffect(() => {
    sk.emit('chatlistToUpdate');
  }, [chatList]);

  return {
    chatList,
    end
  };
};

CodePudding user response:

Try to log your data first to make sure the data is there, then set your state with the data.

const [state, setState]= useState([]);

const _onReceived = (data) => {
  // Here is your data from socket
  console.log(data);

  // Then set state value with data
  setState(data);
}

useEffect(()=>{
  // Init socket listener
  socket.on("event", _onReceived);
}, []);

// This effect will runs everytime state value is set (including when setting default value)
useEffect(()=>{
  // Actual 'state' value
  console.log('State value: ', state);
}, [state]);

==========================

Edit, related to your updated codes in the question

Your onChatListToUpdateReceived function brings empty default value to the listener even later when it’s updated, your listener will still recognize chatList value as an empty string. Try to move out onChatListToUpdateReceived outside useEffect.

const onChatListToUpdateReceived = data => {
  const _data = JSON.parse(data);
  console.log(chatList);//getting only empty array everytime
};

useEffect(() => {
  const onChatListReceived = data => {
    const _data = JSON.parse(data);
    setHistoryLoading(false);
    setChatList(_data);
  };

  sk.on('chatlistToUpdate', onChatListToUpdateReceived);

  return () => {
    sk.off('chatlistToUpdate');
    sk.off('chatlist');
  };
}, []);

useEffect(() => {
  sk.off('chatlistToUpdate');
  sk.on('chatlist', onChatListReceived);
}, [chatList]);
  • Related