xenial-black
xenial-black3w ago

using refetch with filters

@kapa.ai How can I pass filters to a refetch query from queryResult?
2 Replies
kapa.ai
kapa.ai3w ago
Hi @amvdxc. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To pass filters to a refetch query using queryResult in refine, you can utilize the refetch function provided by data-related hooks such as useTable, useForm, or useShow. Here’s how you can do it: 1. Access the refetch function: First, you need to access the refetch function from the queryResult of your hook. 2. Pass filters to the refetch function: When calling the refetch function, you can pass the filters as parameters. Here is an example using the useTable hook:
import { useTable } from "@refinedev/core";

const MyComponent: React.FC = () => {
const { tableQueryResult } = useTable({
resource: "posts",
initialFilters: [
{
field: "title",
operator: "contains",
value: "example",
},
],
});

const { refetch } = tableQueryResult;

const handleRefetch = () => {
refetch({
filters: [
{
field: "title",
operator: "contains",
value: "new value",
},
],
});
};

return (
<div>
<button onClick={handleRefetch}>Refetch with Filters</button>
</div>
);
};
import { useTable } from "@refinedev/core";

const MyComponent: React.FC = () => {
const { tableQueryResult } = useTable({
resource: "posts",
initialFilters: [
{
field: "title",
operator: "contains",
value: "example",
},
],
});

const { refetch } = tableQueryResult;

const handleRefetch = () => {
refetch({
filters: [
{
field: "title",
operator: "contains",
value: "new value",
},
],
});
};

return (
<div>
<button onClick={handleRefetch}>Refetch with Filters</button>
</div>
);
};
In this example: - We use the useTable hook to manage the table data. - We access the refetch function from tableQueryResult. - We define a handleRefetch function that calls refetch with new filters. For more detailed information, you can refer to the Refine FAQ on refetching data (https://refine.dev/docs/guides-concepts/faq/#how-can-i-refetch-data) and the useTable documentation (https://refine.dev/docs/ui-integrations/ant-design/hooks/use-table/#filters). If you need to invalidate specific resources or states, you can also use the useInvalidate hook as described in the useInvalidate documentation (https://refine.dev/docs/core/hooks/data/use-invalidate/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
deep-jade
deep-jade2w ago
Hello @amvdxc ,

 kapa.ai is now available for free at https://refine.dev/docs! To improve our community interaction on the Refine Discord server and keep our focus on Q&A sharing, we invite you to our website to use kapa.ai.