Parallel Queries with useQueries Hook
Illustrate fetching multiple queries in parallel by leveraging the useQueries hook for batched data fetching.
import { useQueries } from 'react-query';
import axios from 'axios';
const fetchUserById = async (userId) => {
const { data } = await axios.get(`/api/user/${userId}`);
return data;
};
const fetchProjectById = async (projectId) => {
const { data } = await axios.get(`/api/project/${projectId}`);
return data;
};
Defines fetching functions using axios for users and projects by their IDs.
const userAndProjectData = useQueries([
{ queryKey: ['user', 1], queryFn: () => fetchUserById(1) },
{ queryKey: ['project', 2], queryFn: () => fetchProjectById(2) }
]);
Uses the useQueries hook to fetch user and project data in parallel by invoking the defined fetching functions.