Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
Not getting updated state value in socket callback react native

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};
};
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda