[英]React how to use setInterval with useEfecct and axios request
我必須在哪里使用 setInterval 來獲取每一秒? 當我嘗試它時,我收到此錯誤 Uncaught SyntaxError: Unexpected identifier
useEffect(() => {
const getHistory = async () => {
const res = await axios.get('/api/payment', {
headers: { Authorization: token },
});
setHistory(res.data);
};
getHistory();
}, [token, delivered]);
您可以為此使用swr
,並且不僅可以從內置的間隔重新驗證中受益,還可以從緩存和重復數據刪除中受益。
import useSWR from 'swr'
// You can use axios if you want
const fetcher = (...args) => fetch(...args).then(res => res.json())
const { data, error } = useSWR('/api/payment', fetcher, { refreshInterval: 1000 })
if (error) return <div>failed to load</div>
if (!data) return <div>loading...</div>
return <div>Look at all this data: {data.someProperty}</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.