MobxQueryClient
An enhanced version of TanStack's Query QueryClient.
Adds specialized configurations for library entities like MobxQuery
or MobxMutation
.
API Signature
import { QueryClient } from "@tanstack/query-core";
class MobxQueryClient extends QueryClient {
constructor(config?: MobxQueryClientConfig);
}
Configuration
When creating an instance, you can provide:
import { DefaultOptions } from '@tanstack/query-core';
interface MobxQueryClientConfig {
defaultOptions?: DefaultOptions & {
queries: MobxQueryFeatures;
mutations: MobxMutatonFeatures;
};
hooks?: MobxQueryClientHooks;
}
Key methods and properties
queryFeatures
Features configurations exclusively for MobxQuery
/MobxInfiniteQuery
mutationFeatures
Features configurations exclusively for MobxMutation
hooks
Entity lifecycle events. Available hooks:
Hook | Description |
---|---|
onQueryInit | Triggered when a MobxQuery is created |
onInfiniteQueryInit | Triggered when a MobxInfiniteQuery is created |
onMutationInit | Triggered when a MobxMutation is created |
onQueryDestroy | Triggered when a MobxQuery is destroyed |
onInfiniteQueryDestroy | Triggered when a MobxInfiniteQuery is destroyed |
onMutationDestroy | Triggered when a MobxMutation is destroyed |
Inheritance
MobxQueryClient
inherits all methods and properties from QueryClient, including:
getQueryData()
setQueryData()
invalidateQueries()
prefetchQuery()
cancelQueries()
- And others (see official documentation)
Usage Example
import { MobxQueryClient } from 'mobx-tanstack-query';
// Create a client with custom hooks
const client = new MobxQueryClient({
hooks: {
onQueryInit: (query) => {
console.log('[Init] Query:', query.queryKey);
},
onMutationDestroy: (mutation) => {
console.log('[Destroy] Mutation:', mutation.options.mutationKey);
}
},
defaultOptions: {
queries: {
enableOnDemand: true,
},
mutations: {
invalidateByKey: true,
}
},
});
// Use standard QueryClient methods
const data = client.getQueryData(['todos']);
When to Use?
Use MobxQueryClient
if you need:
- Customization of query/mutation lifecycle
- Tracking entity initialization/destruction events
- Advanced configuration for
MobX
-powered queries and mutations.