从突变请求中检测新添加的项目

发布于 2025-02-12 11:26:15 字数 209 浏览 1 评论 0原文

我有一个前端,希望将新添加的用户标记为用户(想象一下添加了一个新的待办事项,并且已经进行了拆除,并且该新项目被标记为向用户表明它是“新的”)。

我正在考虑使用LocalStorage以某种方式保存getTodolist结果的旧状态,一旦将此API倒置,它将与旧的和标记新的待办事项进行比较为“ new”。

有没有办法为我发现这一点吗?如果没有,是否有一种更干净的方法来构建它?

I have a frontend that wants to mark to users which items in a list is newly added (imagine a new to-do is added, and it is refetched and this new item is marked to show the user that it is "new").

I am thinking of using localStorage to somehow save the old state of the getToDoList result and once this API is being refetched, it will compare with the old and mark new to-dos as "new".

Is there a way for react-query to detect this for me? If not, is there a cleaner way to build this?

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

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

发布评论

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

评论(1

伴我心暖 2025-02-19 11:26:15

基于React-Query文档,您可以访问带有上下文的旧数据。

 useMutation(addTodo, {
   onMutate: variables => {
     // A mutation is about to happen!
 
     // Optionally return a context containing data to use when for example rolling back
     return { id: 1 }
   },
   onError: (error, variables, context) => {
     // An error happened!
     console.log(`rolling back optimistic update with id ${context.id}`)
   },
   onSuccess: (data, variables, context) => {
     // Boom baby!
   },
   onSettled: (data, error, variables, context) => {
     // Error or success... doesn't matter!
   },
 })

based on react-query documentation, you can access to old data with context.

 useMutation(addTodo, {
   onMutate: variables => {
     // A mutation is about to happen!
 
     // Optionally return a context containing data to use when for example rolling back
     return { id: 1 }
   },
   onError: (error, variables, context) => {
     // An error happened!
     console.log(`rolling back optimistic update with id ${context.id}`)
   },
   onSuccess: (data, variables, context) => {
     // Boom baby!
   },
   onSettled: (data, error, variables, context) => {
     // Error or success... doesn't matter!
   },
 })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文