Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useIsMutating): fix vue warning #5453

Merged
merged 3 commits into from
Jun 14, 2023
Merged
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
16 changes: 12 additions & 4 deletions packages/vue-query/src/useMutationState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onScopeDispose, readonly, computed, ref } from 'vue-demi'
import { onScopeDispose, readonly, computed, ref, watch } from 'vue-demi'
import type { Ref, DeepReadonly } from 'vue-demi'
import type {
MutationFilters as MF,
Expand All @@ -24,9 +24,8 @@ export function useIsMutating(
status: 'pending' as const,
}))

const length = computed(
() => useMutationState({ filters: unreffedFilters }, client).value.length,
)
const mutationState = useMutationState({ filters: unreffedFilters }, client)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why this change is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effective scope does not exist inside the computed. You cannot create reactive variables inside computed.
All reactive variables should be created synchronously from setup function, not from some callbacks.
If you try to create reactive variable without reactive context, you will get a Vue warning:
onScopeDispose() is called when there is no active effect scope to be associated with.

const length = computed(() => mutationState.value.length)

return length
}
Expand Down Expand Up @@ -58,13 +57,22 @@ export function useMutationState<TResult = MutationState>(
options: MutationStateOptions<TResult> = {},
queryClient?: QueryClient,
): DeepReadonly<Ref<Array<TResult>>> {
const filters = computed(() => cloneDeepUnref(options.filters))
const mutationCache = (queryClient || useQueryClient()).getMutationCache()
const state = ref(getResult(mutationCache, options)) as Ref<TResult[]>
const unsubscribe = mutationCache.subscribe(() => {
const result = getResult(mutationCache, options)
state.value = result
})

watch(
filters,
() => {
state.value = getResult(mutationCache, options)
},
{ deep: true },
)

onScopeDispose(() => {
unsubscribe()
})
Expand Down