-
My problemI'm using Here is my codeconst [page, setPage] = useState<number>(1);
// Data returned when setPage inside useEffect called
const { data, isFetching } = useQuery<GetProductsQuery>(
['products', page],
() => getProducts(page),
{
keepPreviousData: true,
refetchOnMount: false,
refetchOnWindowFocus: false,
enabled: Boolean(page),
staleTime: 5 * 1000,
}
);
const initProducts = data?.products.products;
const nextPage = data?.products.nextPage;
// Init data from server side so client can see products without having to see empty page
const [products, setProducts] = useState<ProductType[]>(initProducts ?? []);
const getProducts = async (page: number) => {
const { category, sort, order } = router.query;
const { data } = await apolloClient.query(...)
// my API call here...
return data;
};
// I'm using built in IntersectionObserver Javascript to detect scroll to bottom,
// It's work okay
useEffect(() => {
if (isIntersecting && nextPage && !isFetching) setPage(nextPage);
}, [isFetching, isIntersecting, nextPage]); Hope you understand them, thanks a lot!! 🥪🌮 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
OMG I found a way to solve that with Let's see my code firstconst { data, isFetching } = useQuery<GetProductsQuery>(
['products', page],
() => getProducts(page),
{
// options above...
onSuccess(data) {
setProducts((prevState) => [...prevState, ...data.products]); // It's solved problem above !! 😄😄
},
}
); New issueWhen I click a button to filter products, I wanna reset my I also tried when i click button filter const queryClient = useQueryClient();
queryClient.removeQueries('products'); Do you have any idea, thanks a ton 😄 |
Beta Was this translation helpful? Give feedback.
-
I solved my problem here queryClient.removeQueries('products'); // clear all products queries
setPage(1); // reset page back to 1 |
Beta Was this translation helpful? Give feedback.
I solved my problem here