状态布尔值未与false一起使用。

发布于 2025-01-24 12:23:11 字数 1361 浏览 1 评论 0原文

我有一些功能,可以处理遵循的用户并取消关注另一个用户。

  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?.getAlluserFollowersrefetchfollowers运行后显示一个空数组

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2025-01-31 12:23:12

让我们将两种异步方法包装在React的Usecallback中,从这两个async中删除CheckISTOLLING()调用,因为它是从使用效率调用的,并添加对这些方法的依赖性:

const handleFollowUser = useCallback(async () => {
    try {
      const { data } = await followUser({
        variables: {
          followedByUserId: Number(state.currentUser.id),
          followingUserId: Number(postFromUser.id)
        }
      })
      if (data) {
        //When you receive data, you should set followersData state here. Like, setFollowersData(data). Please, make sure that you have done it properly.
        refetchFollowers()
      }
    } catch (err) {
      console.log(err)
    }
  },[state.currentUser.id, postFromUser.id])

  const handleUnfollowUser = useCallback(async () => {
    try {
      const { data } = await unfollowUser({
        variables: {
          userId: Number(state.currentUser.id),
          userIdToUnfollow: Number(postFromUser.id)
        }
      })
      if (data) {
        refetchFollowers()
      }
    } catch (err) {
      console.log(err)
    }
  }, [state.currentUser.id, postFromUser.id])

之后,让我们修改使用效率的依赖性

useEffect(() => {
    if (followersData) {
      checkIsFollowing()
    }
  }, [followersData, handleFollowUser, handleUnfollowUser])

: 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:

const handleFollowUser = useCallback(async () => {
    try {
      const { data } = await followUser({
        variables: {
          followedByUserId: Number(state.currentUser.id),
          followingUserId: Number(postFromUser.id)
        }
      })
      if (data) {
        //When you receive data, you should set followersData state here. Like, setFollowersData(data). Please, make sure that you have done it properly.
        refetchFollowers()
      }
    } catch (err) {
      console.log(err)
    }
  },[state.currentUser.id, postFromUser.id])

  const handleUnfollowUser = useCallback(async () => {
    try {
      const { data } = await unfollowUser({
        variables: {
          userId: Number(state.currentUser.id),
          userIdToUnfollow: Number(postFromUser.id)
        }
      })
      if (data) {
        refetchFollowers()
      }
    } catch (err) {
      console.log(err)
    }
  }, [state.currentUser.id, postFromUser.id])

After that, let's modify the dependency of useEffect:

useEffect(() => {
    if (followersData) {
      checkIsFollowing()
    }
  }, [followersData, handleFollowUser, handleUnfollowUser])

And, as pilchard said in his comment, you should concentrate on your checkIsFollowing method if it returns correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文