When component renders I send a socket emit with socket io to get data inside useEffect, I also listen to get the data. When i get the data back from socket I update state then call a function. Inside the fuction the state is empty. Why is state empty?
const [chatData, setChatData] = useState([]);
const handleData = () => {
console.log(chatData)
}
useEffect(() => {
socket.emit("chatData");
socket.on("chatData", (data) => {
setChatData(data)
handleData();
});
return () => {
socket.off("chatData");
};
}, []);