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

123
Vistas
Why isnt this setting the value correctly?

The setConversations component is not setting the conversations variable correctly, but it outputs in the console correctly. No idea why its not doing it, its done the same way in other parts of this app another thing to mention, the undefined conversations is outputting before the res.data is being outputted to the console

 const profile = JSON.parse(localStorage.getItem('profile'))
  const userId = profile.result._id
  console.log(userId)
 
  const [conversations, setConversations] = useState([]);
  const [currentChat, setCurrentChat] = useState(null);
  const [messages, setMessages] = useState([]);
  const [newMessage, setNewMessage] =  useState("");
  //const { user } = useContext(AuthContext);
  const scrollRef= useRef();


  useEffect(() => {
    axios.get(`http://localhost:5000/api/v1/conversations/conversation/?userId=${userId}`)
        .then(res => {
          console.log(res.data)
          setConversations(res.data);
        })
      }, [userId])

    console.log(conversations)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Sometimes even after setting the values, it takes time to refresh the component.

So better use the following way while displaying the results which will make sure that the conversations exist and then displayed

Inside JSX code

<div>
{
    conversations && console.log(conversations)
}
</div>
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