Reputation: 3
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)
Upvotes: 0
Views: 39
Reputation: 2927
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>
Upvotes: 1