useInvalidate
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.
The hook is used internally by refine. In most cases, you won't need this hook, but we export it as it may be useful for some use-cases that may require customized invalidation.
refine uses TanStack Query to fetch and manage the state of the data. For more information about invalidation, please read the TanStack Query's invalidation docs.
Basic Usageβ
import { useInvalidate } from "@refinedev/core";
const invalidate = useInvalidate();
invalidate({
resource: "posts",
invalidates: ["list"],
});
Examplesβ
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"],
});
Invalidation Parametersβ
resource
β
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.
id
β
The id
to use when invalidating the "detail"
state.
dataProviderName
β
If there is more than one dataProvider
, you should specify which one to use by passing the dataProviderName
prop.
invalidates
requiredβ
Type:
Array<"all", "resourceAll", "list", "many", "detail", "false">
|false
The states you want to invalidate. You can use the following values:
"all"
: Invalidates all states of the all resources."resourceAll"
: Invalidates all states of the givenresource
."list"
: Invalidates the"list"
state of the givenresource
."detail"
: Invalidates the"detail"
state of the givenresource
andid
."many"
: Invalidates the"many"
state of the givenresource
.
API Referenceβ
Invalidation Parametersβ
Property | Description | Type | Default |
---|---|---|---|
invalidates Required | 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 |