I'm using react query to call an api to retreive a list of schedule dates for users to use to update a list of selected dates from a calendar. However I'm not sure how to do this using react query. Currently the naked setDates is causing too many rerenders because it's not inside eg a useEffect() hook that I would normally use for an api call. Now I'm using react query I'm not sure where to put it?
const SomeDateComponent = ({ }) => {
const [selectedDates, setSelectedDates] = React.useState<selectedDates>({});
.....
const { data: existingSchedules } = useExistingScheduleDates(
startDate,
endDate,
userIds.map((agent) => agent.userId),
);
setDates((selectedDates) => ({ ...selectedDates, ...existingSchedules }))
return (....)
custom Hook:
export function useExistingScheduleDates(
startDate: Date,
endDate: Date,
userIds: number[],
): QueryObserverResult<ExistingScheduleDates> {
return useQuery(
['existingSchedules', startDate, endDate],
() => {
return apiCall( {
route: '/schedulesExist',
query: {
startDate: format(startDate, dateFormat),
endDate: format(endDate, dateFormat),
userId: userIds,
},
});
},
{
refetchOnWindowFocus: false,
},
);
}