我想使用React/JavaScript执行多个fetch请求,我有以下几种try :
const fetchAll = async () => {
Promise.all([
await fetch('/api/...'),
await fetch('/api/...')
]).then(links => {
const response1 = links[0];
const response2 = links[1];
timeData = response1.json();
functionData = response2.json();
})
}
但我想这样做,因为这似乎更有用.如果可能的话,我想使用useffect和useState,并在useState的不同数组中加载不同api的数据.以下是一个示例:
const [data, setData] = useState([]);
useEffect(() => {
fetch("/api/..")
.then((response) => response.json())
.then((r) => {
setData(r);
});
}, []);
是否有一种方法可以对多个请求执行此操作,并将数据保存在不同的数组中,以便我以后可以访问它们?