通过使用useEffect,我的数组状态变量未附加先前的值,但componentDidMount附加了先前的值
在使用useEffect时,我console.log退出消息,并且所有消息均已正确打印,但是状态并未存储,如果我将消息状态变量设置为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,我可以安慰掉所有消息,但是消息状态数组变量为空,所以最终结果只是最新消息。
答案 0 :(得分:0)
由于状态取决于当前状态值,因此需要使用setMessages
的功能形式。这意味着您不需要包括messages
作为依赖项(因为它实际上不是依赖项):
useEffect(() => {
...
chatManager.connect()
.then(currentUser => {
currentUser.subscribeToRoomMultipart({
roomId: '7b7a1d23-e869-4c19-8eab-e88d5144dd72',
hooks: {
onMessage: message => {
setMessages(prevMessages => [...prevMessages, message]);
}
}
})
})
}, [])
有关更多信息,请参见React文档: https://reactjs.org/docs/hooks-reference.html#functional-updates