documentation/docs/data/hooks/use-invalidate/index.md
useInvalidate is a hook that can be used to invalidate the state of a particular resource or dataProvider (with dataProviderName).
This hook will be called when a mutation hook is successful. For example, creating a Posts with the useCreate hook will invalidate the list (useList) and many (useMany) state of the Posts resource.
:::simple Good to know
:::
import { useInvalidate } from "@refinedev/core";
const invalidate = useInvalidate();
// `invalidate` function is async and returns a promise. If you want to wait for the invalidation process to complete, you can await it.
invalidate({
resource: "posts",
invalidates: ["list"],
});
To invalidate the "list" and "many" states of the Posts resource.
invalidate({
resource: "posts",
invalidates: ["list", "many"],
});
To invalidate the state of a Posts with an id of 1.
invalidate({
resource: "posts",
invalidates: ["detail"],
id: 1,
});
To invalidate the "list" and "many" states of the Posts resource of the dataProvider named "second-data-provider".
invalidate({
resource: "posts",
dataProviderName: "second-data-provider",
invalidates: ["list"],
});
To invalidate all states of the dataProvider named "second-data-provider".
invalidate({
dataProviderName: "second-data-provider",
invalidates: ["all"],
});
To invalidate all states of the Posts.
invalidate({
resource: "posts",
invalidates: ["resourceAll"],
});
A resource represents an entity in an endpoint in the API (e.g. https://api.fake-rest.refine.dev/posts). It is used to invalidate the state of a particular resource.
The id to use when invalidating the "detail" state.
If there is more than one dataProvider, you should specify which one to use by passing the dataProviderName prop.
The scope of the invalidation process. These scopes can be provided in an array.
"all": Invalidates all states of the all resources."resourceAll": Invalidates all states of the given resource."list": Invalidates the "list" state of the given resource."detail": Invalidates the "detail" state of the given resource and id."many": Invalidates the "many" state of the given resource.The filters and options applied to the invalidation process when picking which queries to invalidate. By default Refine applies some filters and options to fine-tune the invalidation process.
By default settings, all the targeted queries are invalidated and the active ones are triggered for a refetch. If there are any ongoing queries, they are kept as they are.
| Property | Description | Type | Default |
|---|---|---|---|
| invalidated <PropTag asterisk /> | The states you want to invalidate. | all, resourceAll, list, many, detail, false | |
| resource | Resource name for State invalidation. | string | |
| id | The id to use when invalidating the "detail" state. | BaseKey | |
| dataProviderName | The name of the data provider whose state you want to invalidate. | string | default |
| invalidationFilters | The filters to use when picking queries to invalidate | InvalidateQueryFilters | { type: "all", refetchType: "active" } |
| invalidationOptions | The options to use in the invalidation process | InvalidateOptions | { cancelRefetch: false } |