¿Dónde tengo que usar setInterval para buscar cada segundo? Cuando lo intento, aparece este error Error de sintaxis no detectado: identificador inesperado
useEffect(() => { const getHistory = async () => { const res = await axios.get('/api/payment', { headers: { Authorization: token }, }); setHistory(res.data); }; getHistory(); }, [token, delivered]);Puede usarswr para esto y beneficiarse no solo de la revalidación integrada en el intervalo, sino también del almacenamiento en caché y la deduplicación.
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>