My page has a search form and table with a paginator. So I have two states: searchCondition and pagination. I need to query the channelList when the page component is mounted.
When the user clicks the paginator, the pagination state will be changed so that a new channelList query will be made. That's ok.
The search form includes two input form items and a "Query" button. The channelList query also can be triggered by the user click event.
But now, when the user types something in the input form item. The channelList query is made immediately when the searchCondition state changed. That's not expected. I want this query triggered when the user clicks the "Query" button.
I read the doc Dependent Queries, there is an enabled option, but don't think it can solve my issue.
Currently:
- Send the query on the component mount. - done!
- Made a new query when
paginationstate changed - done! - User types something in the input form item(this will cause the
searchConditionstate changed) and clicks the "Query" button, then made a new query - undone.
When sending the channelList query, I will make the API parameters by merging searchCondition and pagination states.
const { searchCondition, setSearchCondition } = useSearchCondition(initialSearchCondition);
const { pagination, setPagination } = 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 },
);