Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save brandonbryant12/7fe09023067982338750b6efbb336abe to your computer and use it in GitHub Desktop.

Select an option

Save brandonbryant12/7fe09023067982338750b6efbb336abe to your computer and use it in GitHub Desktop.
import { useQueries } from '@tanstack/react-query';
function useAggregatedIncidents() {
const queryClient = useQueryClient();
const groupIds = ['group1', 'group2', 'group3'];
// Dynamically create queries for each group
const groupQueries = useQueries({
queries: groupIds.map((groupId) => ({
queryKey: ['incidents', groupId],
queryFn: () => fetchIncidentsByGroup(groupId), // Your API call
staleTime: 5 * 60 * 1000, // Adjust based on your needs
})),
});
const aggregatedData = useMemo(() => {
let allIncidents = [];
groupQueries.forEach((query) => {
if (query.data) {
allIncidents = [...allIncidents, ...query.data];
}
});
return {
totalIncidents: allIncidents.length,
isLoading: groupQueries.some((query) => query.isLoading),
};
}, [groupQueries]);
return aggregatedData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment