状态布尔值未与false一起使用。
我有一些功能,可以处理遵循的用户并取消关注另一个用户。
const handleFollowUser = async () => {
try {
const { data } = await followUser({
variables: {
followedByUserId: Number(state.currentUser.id),
followingUserId: Number(postFromUser.id)
}
})
if (data) {
refetchFollowers()
checkIsFollowing()
}
} catch (err) {
console.log(err)
}
}
const handleUnfollowUser = async () => {
try {
const { data } = await unfollowUser({
variables: {
userId: Number(state.currentUser.id),
userIdToUnfollow: Number(postFromUser.id)
}
})
if (data) {
refetchFollowers()
checkIsFollowing()
}
} catch (err) {
console.log(err)
}
}
检查符号
搜索用户关注者列表,并检查那里的当前用户ID是否。这总是在页面加载上正常工作。
const checkIsFollowing = () => {
followersData?.getAllUserFollowers.some((follower) => {
setIsFollowing(follower.id === state.currentUser.id)
})
}
useEffect(() => {
if (followersData) {
checkIsFollowing()
}
}, [followersData])
我的问题是,当我取消关注用户时,布尔值iSfollowing
并没有回到false。当我最初关注用户时,它会正确地翻转,但如果我取消关注他们,则不会。即使collesdata?.getAlluserFollowers
在refetchfollowers
运行后显示一个空数组
I have some functions which handle a user following and unfollowing another user.
const handleFollowUser = async () => {
try {
const { data } = await followUser({
variables: {
followedByUserId: Number(state.currentUser.id),
followingUserId: Number(postFromUser.id)
}
})
if (data) {
refetchFollowers()
checkIsFollowing()
}
} catch (err) {
console.log(err)
}
}
const handleUnfollowUser = async () => {
try {
const { data } = await unfollowUser({
variables: {
userId: Number(state.currentUser.id),
userIdToUnfollow: Number(postFromUser.id)
}
})
if (data) {
refetchFollowers()
checkIsFollowing()
}
} catch (err) {
console.log(err)
}
}
checkIsFollowing
searches a users followers list and checks if the current user ID in there. This always works properly on page load.
const checkIsFollowing = () => {
followersData?.getAllUserFollowers.some((follower) => {
setIsFollowing(follower.id === state.currentUser.id)
})
}
useEffect(() => {
if (followersData) {
checkIsFollowing()
}
}, [followersData])
The problem im having is when I unfollow a user the boolean isFollowing
is not flipping back to false. It flips properly when I follow the user initially but not if I unfollow them. Even though followersData?.getAllUserFollowers
shows an empty array after refetchFollowers
runs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们将两种异步方法包装在React的Usecallback中,从这两个async中删除CheckISTOLLING()调用,因为它是从使用效率调用的,并添加对这些方法的依赖性:
之后,让我们修改使用效率的依赖性
: Pilchard在他的评论中说,如果正确返回,您应该专注于遵守方法。
Let's wrap the two async method within useCallback of react, remove the checkIsFollowing() call from within these two async as it is being called from useEffect, and adding dependency to these methods:
After that, let's modify the dependency of useEffect:
And, as pilchard said in his comment, you should concentrate on your checkIsFollowing method if it returns correctly.