Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!