我的API密钥限制我在5秒内发出超过1个请求,所以我想在发出另一个NearbyJobs请求之前等待5秒(第一个请求是PopularJobs)。
<ScrollView showsVerticalScrollIndicator={false} horizontal={false}>
<View style={{ flex: 1, padding: SIZES.medium }}>
<Welcome />
<Popularjobs />
{/* {setTimeout(() => {}, 6000)} */}
<Nearbyjobs />
</View>
</ScrollView>
API调用
import { useState, useEffect } from "react";
import axios from "axios";
const useFetch = (endpoint, query) => {
const [data, setData] = useState([]);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const options = {
method: "GET",
url: `https://jsearch.p.rapidapi.com/${endpoint}`,
params: { ...query },
headers: {
"X-RapidAPI-Key": "",
"X-RapidAPI-Host": "jsearch.p.rapidapi.com",
},
};
const fetchData = async () => {
setLoading(true);
try {
const response = await axios.request(options);
setData(response.data.data);
} catch (err) {
setError(err);
alert(err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, []);
const refetch = () => {
setLoading(true);
fetchData();
};
return { data, error, loading, refetch };
};
export default useFetch;
1条答案
按热度按时间7gyucuyw1#
你能尝试在
useEffect
中使用setTimeout
吗?