【发布时间】:2020-07-23 01:32:07
【问题描述】:
我将 ReactJS 用作带有 JSX 的 javascript 库,并且我正在使用 axios 获取数据。接收到的数据必须每 5 秒重新采集一次。最有效和最好的方法是什么?
我正在考虑使用 setInterval(),但不确定该方法应该放在哪里。
import React, { useState, useEffect } from 'react';
import axios from 'axios'
import './App.css';
const App = () => {
const [results, setResults] = useState([])
useEffect(()=>{
axios.get('API LINK')
.then(res => {
setResults(res.data.result)
});
},[]);
const Results = () => {
return (
<ul>
{results.map( (i)=>(<li key={i.sys_id}><h1>{i.u_dashboard}</h1>
Calls in Queue (CIQ): {i.u_calls_in_queue}
{"\n"}Longest Active Call (LAC): {i.u_longest_active_call}</li>)) }
</ul>
)
}
【问题讨论】:
标签: javascript reactjs api axios jsx