How to refetch query with params #351
-
For example I created a query This data used for select component and I wonna refetch it with |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 40 replies
-
I was wondering this as well |
Beta Was this translation helpful? Give feedback.
-
Recommended: Turn your variables into a permanent query key part and just update them with state or props useQuery(['data_sources', { variables }], getDataSources)
// or
useQuery(['data_sources', variables], getDataSources) |
Beta Was this translation helpful? Give feedback.
-
Login Is not a query, it’s a mutation. Use useMutation instead.
Tanner Linsley
…On Apr 15, 2021, 7:04 AM -0600, Bert Catsburg ***@***.***>, wrote:
Hello TkDodo,
Thanks for your explanation and technically you are absolutely right.
But it is fairly standard behaviour that you have a form, fill in some fields and want to do a query with those fields as input.
For example a login-form where you input email,password and want to get the JWT-token back.
Currently this requires a submitHandler, a useState, a useEffect and a useQuery. That's a lot of code for doing a simple query.
If somebody can write the above functionality in a better and more simple way (and put it in the docs!!) it would clarify a lot.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
@TkDodo Hi, can you take a look at my issue? Thanks. I hit this issue by using My page has a search form and table with a paginator. So I have two states: Currently:
The problem is: When user types something in inputs and click the "Query" button, the For example, I am currently on page 3, when the user types something and clicks the "Query" button. The
It's correct. Because the const { searchCondition, setSearchCondition } = useSearchCondition(initialSearchCondition);
const { pagination, setPagination, resetPagination } = usePagination();
const channelListQuery = useQuery(
['channelList', pagination, searchCondition],
async () => {
const getChannelListParams = omitFalsyFields({
currentPage: pagination.current,
pageSize: pagination.pageSize,
...searchCondition,
});
const { code, result } = await apis.channel.getChannelList(getChannelListParams);
if (code === '0') {
return result;
}
},
{ initialData: { totalItem: 0, resultList: [] }, refetchOnWindowFocus: false, keepPreviousData: true, enabled: false, cacheTime: 0 },
);
useEffect(() => {
channelListQuery.refetch();
}, [pagination]);
const handleSearchFormSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
resetPagination();
channelListQuery.refetch();
};
// ...
pagination={{
total: channelListQuery.data.totalItem,
...pagination,
showQuickJumper: true,
showSizeChanger: true,
onChange: (page: number, pageSize?: number) => {
const nPagination = { current: page, pageSize: pageSize || paginationInitialState.pageSize };
setPagination(nPagination);
},
}} Maybe useEffect(() => {
channelListQuery.refetch();
}, []);
const handleSearchFormSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
channelListQuery.refetch({...paginationInitialState, ...searchCondition});
};
// pagination
onChange: (page: number, pageSize?: number) => {
const nPagination = { current: page, pageSize: pageSize || paginationInitialState.pageSize };
setPagination(nPagination);
channelListQuery.refetch({...nPagination, ...searchCondition});
}, So how does |
Beta Was this translation helpful? Give feedback.
-
const {
refetch: orderInvoiceRefetch,
} = useQuery(
["order-invoice"],
(orderId) => axios
.get(`/orders/invoice?order_id=${orderId}`)
.then(({ data })) => data),
{ enabled: false }
); I need to pass orderInvoiceRefetch({
queryKey: id,
}); How can I pass a parameter to the |
Beta Was this translation helpful? Give feedback.
Recommended: Turn your variables into a permanent query key part and just update them with state or props