Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/solid-query-mutation-state-untrack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/solid-query': patch
---

fix(solid-query): stop `useMutationState` making the computation that triggers a mutation depend on its result

The mutation-cache subscription read the hook's own result signal. `MutationCache` notifies synchronously, so that read happened while the computation that called `mutate` was still the active listener — registering the signal as one of its dependencies, which `setResult` then immediately invalidated. Calling `mutate` from inside an effect therefore re-ran that effect on every mutation, and an unguarded `mutate` looped. The read is now untracked.
32 changes: 32 additions & 0 deletions packages/solid-query/src/__tests__/useMutationState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,36 @@ describe('useMutationState', () => {

expect(variables).toEqual([[], [1], []])
})

it('should not make the computation that triggers a mutation depend on its result', async () => {
const mutationKey = queryKey()
let effectRuns = 0

function Page() {
const states = useMutationState(() => ({ filters: { mutationKey } }))
const mutation = useMutation(() => ({
mutationKey,
mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
}))

createEffect(() => {
effectRuns++
// Guarded so that a genuine feedback loop still terminates the test.
if (effectRuns === 1) {
mutation.mutate(1)
}
})

return <div>count: {states().length}</div>
}

renderWithClient(queryClient, () => <Page />)
await vi.advanceTimersByTimeAsync(150)

// `mutate` notifies the mutation cache synchronously, so the subscription
// callback runs while this effect is the active computation. Reading the
// result signal there used to register it as a dependency of that effect,
// which `setResult` then immediately invalidated.
expect(effectRuns).toBe(1)
})
})
16 changes: 13 additions & 3 deletions packages/solid-query/src/useMutationState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { createEffect, createMemo, createSignal, onCleanup } from 'solid-js'
import {
createEffect,
createMemo,
createSignal,
onCleanup,
untrack,
} from 'solid-js'
import { replaceEqualDeep } from '@tanstack/query-core'
import { useQueryClientResolver } from './QueryClientProvider'
import type {
Expand Down Expand Up @@ -66,11 +72,15 @@ export function useMutationState<

createEffect(() => {
const unsubscribe = mutationCache().subscribe(() => {
// `subscribe` invokes this synchronously, so reading `result` while the
// enclosing effect is still tracking would make the effect depend on the
// signal it sets, re-running and re-subscribing on every mutation.
const previousResult = untrack(result)
const nextResult = replaceEqualDeep(
result(),
previousResult,
getResult(mutationCache(), options()),
)
if (result() !== nextResult) {
if (previousResult !== nextResult) {
setResult(nextResult)
}
})
Expand Down