使用Wrinfinite与分页和突变特征

发布于 2025-02-11 00:06:55 字数 2240 浏览 2 评论 0原文

我正在使用usewr来从NextJ中的客户端获取数据。

我正在做的事情并试图实现

我正在使用useswrinfinite进行分页功能,并尝试更新具有OpptisticData的绑定突变功能之类的评论,因为我想立即刷新数据。(客户端侧透视)

- > https://swr.vercel.app/docs/munt.unt.popt-poptimistic-umistic-updates 然后从Axios中获取新的更新评论,并将其替换为以前的评论,该评论应更新。

预期

usesingwrinfinite的数据应立即更新,因为我正在使用OpportisticData选项,直到完成API调用为止,并且我可以将RETAIRES OPTICE设置为True,但是在突变返回中的异步功能更新了,该函数带有带有带有的更新数据。 Axios的响应。我不需要。

实际行为

即使我将OpportisticData传递给突变,也不会立即更新数据。它一直在等待直到完成API调用,然后更新。

我尝试过的

我尝试过仅使用分页功能的普通使用WRWARD,并且按照我的预期运行良好。

const { data, error, isValidating, mutate, size, setSize } = useSWRInfinite<CommentType[]>(
   (index) => `/api/comment?postId=${postId}&currentPage=${index + 1}`,
   fetcher,
   { revalidateFirstPage: false }
);

  const likeCommentHandler = async (commentId: string, dislike: boolean) => {
    const optimisticData = data?.map((comments) => {
      return comments.map((comment) => {
        if (comment.id === commentId) {
          if (dislike) {
            --comment._count.likedBy;
            comment.likedByIds = comment.likedByIds.filter(
              (likeById) => likeById !== session!.user.id
            );
          } else {
            comment.likedByIds.push(session!.user.id);
            ++comment._count.likedBy;
          }
          return { ...comment };
        } else {
          return { ...comment };
        }
      });
    });

    mutate(
      async (data) => {
        const { data: result } = await axios.post("/api/likeComment", {
          commentId: commentId,
          userId: session?.user.id,
          dislike,
        });

        const newData = data?.map((comments) => {
          return comments.map((comment) => {
            if (comment.id === result.comment.id) {
              return result.comment;
            } else {
              return comment;
            }
          });
        });

        return newData;
      },
      { optimisticData, revalidate: false, populateCache: true }
    );
  };

I'm using useSWR to fetch data from client side in nextjs.

What I am doing and trying to achieve

I am using useSWRInfinite for the pagination feature and trying to update comments like state with bound mutate function with optimisticData option since I wanted to refresh the data immediately.(client-side perspective)

-> https://swr.vercel.app/docs/mutation#optimistic-updates and then get a new updated comment from axios and replace it with a previous comment that should be updated.

Expected

The data from useSWRInfinite should be updated right away since I am using optimisticData option until the API call is done and I could've set revalidate option to true but an async function in the mutate returns updated data with the response from axios. I didn't need it.

Actual behaviour

Even though I am passing optimisticData to the mutate, It doesn't update the data immediately. It keeps waiting until The API call is done and then gets updated.

What I've tried

I have tried using just normal useSWR function without the pagination feature and it worked well as I expected.

const { data, error, isValidating, mutate, size, setSize } = useSWRInfinite<CommentType[]>(
   (index) => `/api/comment?postId=${postId}¤tPage=${index + 1}`,
   fetcher,
   { revalidateFirstPage: false }
);

  const likeCommentHandler = async (commentId: string, dislike: boolean) => {
    const optimisticData = data?.map((comments) => {
      return comments.map((comment) => {
        if (comment.id === commentId) {
          if (dislike) {
            --comment._count.likedBy;
            comment.likedByIds = comment.likedByIds.filter(
              (likeById) => likeById !== session!.user.id
            );
          } else {
            comment.likedByIds.push(session!.user.id);
            ++comment._count.likedBy;
          }
          return { ...comment };
        } else {
          return { ...comment };
        }
      });
    });

    mutate(
      async (data) => {
        const { data: result } = await axios.post("/api/likeComment", {
          commentId: commentId,
          userId: session?.user.id,
          dislike,
        });

        const newData = data?.map((comments) => {
          return comments.map((comment) => {
            if (comment.id === result.comment.id) {
              return result.comment;
            } else {
              return comment;
            }
          });
        });

        return newData;
      },
      { optimisticData, revalidate: false, populateCache: true }
    );
  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文