const [questionIndex, setQuestionIndex] = useState(0);
...
socket.on('next', () => {
console.log('hey')
setQuestionIndex(questionIndex + 1);
});
...
useEffect(() => {
console.log(questionIndex);
}, [questionIndex]);
我有一个使用 Socket.io 连接到 websocket 的页面。当我从套接字收到“下一个”消息时,我试图更新我的 questionIndex 状态变量的值。我似乎收到了消息,因为打印了“嘿”,但 questionIndex 仅在第一个“嘿”之后更新。之后,嘿被打印,但 questionIndex 不是(我使用 useEffect 在更新时打印 questionIndex)。大家有没有看出哪里不对劲?