Created
July 23, 2025 13:37
-
-
Save brandonbryant12/7fe09023067982338750b6efbb336abe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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