Skip to main content

Query and Mutation

@vef-framework-react/core re-exports TanStack React Query with VEF-specific type wrappers and a pre-configured QueryClient.

createQueryClient

Creates a QueryClient with mutation cache and default options.

import { createQueryClient } from "@vef-framework-react/core";

const queryClient = createQueryClient({
staleTime: 5_000,
gcTime: 300_000,
showSuccessMessage: message => notification.success(message)
});

QueryClientOptions

OptionTypeDefaultDescription
staleTimenumber5000Time before data is considered stale (ms)
gcTimenumber300000Time before inactive queries are garbage collected (ms)
showSuccessMessage(msg) => voidCallback to display mutation success messages

useQuery

import { useQuery } from "@vef-framework-react/core";

const result = useQuery({
queryKey: [findUserPage.key, searchParams],
queryFn: findUserPage
});

useMutation

import { useMutation } from "@vef-framework-react/core";

const mutation = useMutation({
mutationKey: [createUser.key],
mutationFn: createUser,
meta: {
invalidates: [[findUserPage.key]],
shouldShowSuccessFeedback: true
}
});

Mutation Meta Extensions

VEF extends TanStack's MutationMeta with two fields:

FieldTypeDescription
invalidatesQueryKey[]Query keys to invalidate on successful mutation
shouldShowSuccessFeedbackbooleanWhether to show success message (default: true)

useInfiniteQuery

import { useInfiniteQuery } from "@vef-framework-react/core";

const result = useInfiniteQuery({
queryKey: [findUserPage.key, searchParams],
queryFn: findUserPage,
initialPageParam: 1,
getNextPageParam: (lastPage, pages) => pages.length + 1
});

useQueries

import { useQueries } from "@vef-framework-react/core";

const results = useQueries({
queries: [
{ queryKey: [findUserPage.key, params1], queryFn: findUserPage },
{ queryKey: [findRoleList.key], queryFn: findRoleList }
]
});

useMutationState

import { useMutationState } from "@vef-framework-react/core";

const states = useMutationState({
filters: { mutationKey: [createUser.key] }
});

useIsFetching and useIsMutating

Low-level hooks for checking global fetch/mutation state. In most cases, prefer useHasFetching and useHasMutating from @vef-framework-react/hooks which accept typed query/mutation keys.

keepPreviousData

import { keepPreviousData } from "@vef-framework-react/core";

useQuery({
queryKey: [findUserPage.key, searchParams],
queryFn: findUserPage,
placeholderData: keepPreviousData
});

skipQueryToken

import { skipQueryToken } from "@vef-framework-react/core";

useQuery({
queryKey: [findUserPage.key, searchParams],
queryFn: searchParams ? findUserPage : skipQueryToken
});

matchQuery and matchMutation

Helpers for filtering queries and mutations in cache operations:

import { matchQuery, matchMutation } from "@vef-framework-react/core";

Type Exports

TypeDescription
UseQueryResult<TData>Result type of useQuery
DefinedUseQueryResult<TData>Result type when initial data is defined
UseQueryOptions<TQueryFnData, TData, TParams>Options for useQuery
DefinedInitialDataOptionsOptions when initial data is always defined
UndefinedInitialDataOptionsOptions when initial data may be undefined
UseInfiniteQueryOptionsOptions for useInfiniteQuery
UseMutationResult<TData, TParams>Result type of useMutation
QueryKeyHashFunctionCustom query key hash function type
PlaceholderDataFunctionType for placeholderData callback
RefetchOptionsOptions for refetch()
StaleTimeType for staleTime option
MutationMetaExtended mutation meta type
QueryMetaQuery meta type
SkipQueryTokenType of skipQueryToken