Possible to make particular instance of useQuery disabled after initial fetch? #2270
-
I have an use case where i fetch data using useQuery and then pass it on to EditForm. Upon successful edit, the query get invalidated the page redirects. Is it possible to make the particular useEntryQuery not subscribe to the invalidation updates? function EditPage() {
const queryClient = useQueryClient();
const entryQuery = useEntryQuery();
const editMutation = useEditMutation({
onSuccess: () => {
queryClient.invalidateQueries("entry");
doRedirection();
}
});
function handleSubmit() {
editMutation.mutate();
}
return <EditForm initialValues={entryQuery.data} onSubmit={handleSubmit} />
} I suppose one way to go around it is to disable the query as soon as it becomes successful. But I'm hoping there's something built-in to handle this kind of scenario. function EditPage() {
const [isQueryEnabled, setIsQueryEnabled] = React.useState(true);
const entryQuery = useEntryQuery({
onSuccess: () => setIsQueryEnabled(false),
enabled: isQueryEnabled
});
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
what are you trying to achieve by disabling the query? once you do the redirect, I assume the |
Beta Was this translation helpful? Give feedback.
-
I think you are looking for:
from the docs:
|
Beta Was this translation helpful? Give feedback.
-
Okay. I've added support for custom filters to the hook. This allows user to override it if necessary. Thanks @TkDodo for your help! export function useCreateEntryMutation(
options,
invalidateQueriesFilters // Allow custom filters
) {
const queryClient = useQueryClient();
return useMutation(
async (params) => {
const { data } = await createEntry(params);
return data!;
},
{
...options,
onSuccess: (...args) => {
queryClient.invalidateQueries('entries', invalidateQueriesFilters);
options?.onSuccess?.(...args);
},
}
);
} |
Beta Was this translation helpful? Give feedback.
I think you are looking for:
from the docs: