Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

useMutationState does not update when filters change (react-query)

未关闭
#11,272 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@MahathirMohammadShuvo 已经在做这个了。

开始于 2026年8月29日。

  • #11273 来自 @alex-js-ltd —— 已关闭,未合并
  • #11281 来自 @dikshit-n —— 已关闭,未合并
  • #11329 来自 @MahathirMohammadShuvo —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript
领域
frontend, testing

调研方向

先从 useMutationState 的实现及其 getSnapshot 和 mutationCache.subscribe 逻辑开始,然后将回归用例添加到 packages/react-query/src/tests/useMutationState.test.tsx。运行 pnpm nx run @tanstack/react-query:test:lib -- useMutationState。当筛选条件发生变化但缓存未更新时,结果从 variables: 1,2 更新为 variables: 1,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Describe the bug

useMutationState() ignores changes to filters. The result is only recomputed when the mutation cache notifies, so after a filter change the array keeps showing the old filters' results until something unrelated touches the cache. If nothing does, it stays wrong.

result.current is only written inside the mutationCache.subscribe() callback, and getSnapshot is () => result.current. So a render with new options recomputes nothing.

Same symptom as #11152 / #11153 in svelte-query, different cause.

Your minimal, reproducible example

N/A, reproduces in the package's own vitest suite.

Steps to reproduce

Add to packages/react-query/src/__tests__/useMutationState.test.tsx, run pnpm nx run @tanstack/react-query:test:lib -- useMutationState:

it('should update the result when mutation filters change without a cache update', async () => {
  const queryClient = new QueryClient()
  const key1 = queryKey()
  const key2 = queryKey()

  function Variables({ mutationKey }: { mutationKey?: Array<string> }) {
    const variables = useMutationState({
      filters: { mutationKey },
      select: (mutation) => mutation.state.variables,
    })

    return <div>variables: {variables.join(',')}</div>
  }

  function Page({ mutationKey }: { mutationKey?: Array<string> }) {
    const { mutate: mutate1 } = useMutation({
      mutationKey: key1,
      mutationFn: (input: number) => sleep(100).then(() => 'data' + input),
    })
    const { mutate: mutate2 } = useMutation({
      mutationKey: key2,
      mutationFn: (input: number) => sleep(100).then(() => 'data' + input),
    })

    React.useEffect(() => {
      mutate1(1)
      mutate2(2)
    }, [mutate1, mutate2])

    return <Variables mutationKey={mutationKey} />
  }

  const rendered = renderWithClient(queryClient, <Page mutationKey={undefined} />)

  await vi.advanceTimersByTimeAsync(0)
  expect(rendered.getByText('variables: 1,2')).toBeInTheDocument()

  rendered.rerender(<Page mutationKey={key1} />)

  expect(rendered.getByText('variables: 1')).toBeInTheDocument()
})

Both mutations stay pending, so the cache never notifies. Output stays variables: 1,2 instead of variables: 1.

Expected behavior

The array should always reflect whatever filters currently select, as it does in vue-query, angular and lit.

How often does this bug happen?

Every time

Platform

N/A, jsdom.

Tanstack Query adapter

react-query

TanStack Query version

v5.102.2

TypeScript version

v6.0.3

主要语言
TypeScript
星标
50.4k
派生
4.2k
平均合并
7 小时 29 分钟
30 天内合并 PR
393

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/query 的其他 Issue

查看 TanStack/query 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。