我有这个代码
const [messages, setMessages] = useState([]);
和
useEffect(() => {
socket.current.on("newMessage", data => {
console.log(messages);
setMessages(messages => messages.concat(data));
let chatContent = document.getElementById('chat-content');
chatContent.scrollTop = chatContent.scrollHeight;
});
},[])
和
{messages.map(message => (
<div className="single-pm">
<div
className={`row ${message.sender.id !== props.location.state.id && 'row-friend'}`}>
<div className="col-md-1">
{message.sender.sex === "1" ? <img src="/images/person.png" alt=""/> :
<img src="/images/girl.png" alt=""/>}
</div>
<div
className={`col-md-11 ${message.sender.id !== props.location.state.id && 'col-friend'}`}>
<p style={{position:'relative'}}>
<span className="sender_name"> {message.sender.name}</span>
<span onClick={() => deleteMsg(message)} className="deleteMsg"><i
className="fas fa-trash"></i></span>
<span onClick={() => editMsg(message)} className="EditMsg"><i
className="fas fa-pencil-alt"></i></span>
{message.msg}
<span
className="date">{new Date(message.date).toLocaleDateString('fa-IR')}</span>
</p>
</div>
</div>
</div>
))}
当我发送新消息时,它处于状态,我可以渲染它,但是在useEffect中看不到它,当我登录时显示给我[] 我该怎么办?