通过使用 useEffect,我的数组状态变量没有附加上一个值,但是 componentDidMount 做到了
我在使用useEffect时console.log out消息,并且所有消息都被正确打印出来,但是状态没有被存储,如果我将messages状态变量设置为useEffect的依赖项,它会进入无限循环
// with useEffect hook
function App() {
const [messages, setMessages] = useState([]);
useEffect(() => {
const chatManager = new ChatManager({
instanceLocator: instanceLocator,
userId: 'Henry',
tokenProvider: new TokenProvider({
url: tokenUrl
})
});
chatManager.connect()
.then(currentUser => {
currentUser.subscribeToRoomMultipart({
roomId: '7b7a1d23-e869-4c19-8eab-e88d5144dd72',
hooks: {
onMessage: message => {
console.log([...messages]);
setMessages([...messages, message]);
}
}
})
}).catch(err => {
console.log('Error on connection', err)
})
}, [])
return (
<div className="app">
<RoomList />
<MessageList messages={messages}/>
<SendMessageForm />
<NewRoomForm />
</div>
);
}
export default App;
// with componentDidMount
class App extends React.Component {
constructor() {
super()
this.state = {
messages: []
}
}
componentDidMount() {
const chatManager = new ChatManager({
instanceLocator,
userId: 'Henry',
tokenProvider: new TokenProvider({
url: tokenUrl
})
})
chatManager.connect()
.then(currentUser => {
currentUser.subscribeToRoomMultipart({
roomId: '7b7a1d23-e869-4c19-8eab-e88d5144dd72',
hooks: {
onMessage: message => {
this.setState({
messages: [...this.state.messages, message]
})
}
}
})
})
}
render() {
return (
<div className="app">
<RoomList />
<MessageList messages={this.state.messages} />
<SendMessageForm />
<NewRoomForm />
</div>
);
}
}
export default App
我希望使用 useEffect 的结果与 componentDidMount 相同,它对消息状态数组变量中的所有消息进行哈希处理,但是通过使用 useEffect,我可以控制台输出所有消息,但消息状态数组变量为空,因此最终结果仅为最新消息。