-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't get latest Redux store entity after dispatch, inside callback. #33
Comments
Observation: It's quite crazy to code like this.
|
Todo:
|
Hi! @sanjarcode I'm also facing same issue pasting below code var lastMessageId = chatThreadFromCache?.messages[chatThreadFromCache?.messages?.length - 1]?._id
function fetchFreshLatestChats(token, chatRoomId) {
trigger({ token, chatRoomId, _id : lastMessageId }, false)
.then(responseLatestMessage => {
dispatch(toggleNewMessageRecieved())
if (responseLatestMessage?.data?.messages?.length > 0) {
// console.log(responseLatestMessage?.messages[0]._id, chatThreadFromCache?.messages.at(-1)?._id)
if (responseLatestMessage?.data?.messages[0]._id !== lastMessageId) {
dispatch(updateThread({ chatRoomId, newMessage: responseLatestMessage?.data?.messages}));
} else console.log("Encountered same.")
} else {
}
setSeenToServer({ token, roomId: chatRoomId })
}).catch(e => console.log("Error While fetching latest message", e))
}
useEffect(() => {
if (newMessageStatus === true) {
fetchFreshLatestChats(token, chatRoomId)
} else {
console.log("")
}
}, [newMessageStatus] ); In the above code what I'm doing basically passing last chat message Id, and getting latest chat but issue is that sometimes I get the same last chat id and hence I get repeated message please suggest me solution I'm very new to react-redux |
you don't have a redux problem, since you check In my case I change the store, but still want to work with the changed data. That is different. Try to replace |
Situation + Problem
The following code doesn't work.
Observations:
Redux:
Workaround ('solution')
Use
useEffect
, with a boolean state (useState
), and move the logic after 'dispatch' in theuseEffect
.Code:
The text was updated successfully, but these errors were encountered: