I call an apollo lazy query hook for a search bar that I made and when I clear the data in the search bar I want to clear the data variable from the hook. I cant anything online to see if this is possible or not
const [
getAutoComplete,
{ data: autoCompleteItems, loading: loadingAutoComplete },
] = useLazyQuery(SEARCH, {
variables: { limit: 10 },
});
And I call the getAutoComplete
here:
useEffect(() => {
if (searchQuery.length > 1 && searchQuery !== '') {
getAutoComplete({ variables: { criteria: searchQuery } });
}
if (searchFocus && searchQuery === '')
// In here I want to do something like set autoCompleteItems = undefined
getAutoComplete({ variables: { criteria: '{}' } });
}
}, [searchQuery]);
Basically when I clear the searchQuery, the autoCompleteItems variable is still using the previous. But when it is empty I want it to be undefined preferably not even call the api.