Samyak
Samyak
NNovu
Created by Samyak on 6/23/2024 in #💬│support
Setting custom queryKey in useFetchNotifications
Hi @Pawan Jain , I'm trying to fetch all notifications and only unread notifications separately. I'm doing something like
const {
data: unreadData,
isLoading: isUnreadLoading,
hasNextPage: hasUnreadNextPage,
fetchNextPage: fetchUnreadNextPage,
} = useFetchNotifications(
{
query: {
limit: 10,
read: false,
},
},
{
refetchOnMount: true,
refetchOnWindowFocus: true,
onError: (err) => handleError(err),
}
);

const {
data: allData,
isLoading: isAllLoading,
hasNextPage: hasAllNextPage,
fetchNextPage: fetchAllNextPage,
} = useFetchNotifications(
{ query: { limit: 10 } },
{
refetchOnMount: true,
refetchOnWindowFocus: true,
onError: (err) => handleError(err),
}
);
const {
data: unreadData,
isLoading: isUnreadLoading,
hasNextPage: hasUnreadNextPage,
fetchNextPage: fetchUnreadNextPage,
} = useFetchNotifications(
{
query: {
limit: 10,
read: false,
},
},
{
refetchOnMount: true,
refetchOnWindowFocus: true,
onError: (err) => handleError(err),
}
);

const {
data: allData,
isLoading: isAllLoading,
hasNextPage: hasAllNextPage,
fetchNextPage: fetchAllNextPage,
} = useFetchNotifications(
{ query: { limit: 10 } },
{
refetchOnMount: true,
refetchOnWindowFocus: true,
onError: (err) => handleError(err),
}
);
I believe useInfiniteQuery underneath useFetchNotifications is using infinite_notifications as queryKey for both requests. Hence, returning the same cached notifications. I was wondering if there's a way to modify the queryKey for each request to fetch all notifications and unread notifications in separate requests. Thanks!
9 replies