You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RTK-Query中updateQueryData回调无控制台输出的问题排查

问题原因及解决方案

核心原因

你调用NotificationsApi.util.updateQueryData时,第二个参数传了undefined,但notificationsAll这个Query是**带参数(user_id)**的。RTK-Query需要通过这个参数匹配对应的缓存条目,找不到匹配的缓存时,就不会执行传入的回调函数(也就是你写的console.log('DISPATCH')所在的逻辑)。

修复代码

把updateQueryData的第二个参数替换成当前mutation拿到的user_id参数,让RTK-Query能定位到正确的缓存:

const patchResult = dispatch(
  NotificationsApi.util.updateQueryData('notificationsAll', { user_id }, (draft) => {
      console.log('DISPATCH');
      // 补充删除对应通知的逻辑:
      const targetIndex = draft.findIndex(item => item.notification_id === notification_id);
      if (targetIndex !== -1) {
        draft.splice(targetIndex, 1);
      }
  })
)

额外验证点

如果修复后仍无效果,确认:

  • notificationsAll Query已经被调用过,且存在对应的缓存数据(可通过Redux DevTools查看缓存状态)
  • 当前传入的user_id和调用notificationsAll时使用的user_id完全一致

内容的提问来源于stack exchange,提问作者locklock123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 04:50:46