【问题标题】:How to set interval in useEffect in React Functional Component如何在 React 功能组件的 useEffect 中设置间隔
【发布时间】:2021-06-02 19:15:47
【问题描述】:
import './App.css';
import { useState, useEffect } from 'react'
import Axios from 'axios'

function App() {

const [list, setList] = useState([]);

useEffect(() => {
    Axios.get('http://localhost:3001/getdata').then((response) => {
        setList(response.data)
    });
})

return (
    <div className="App">
        <div className="container">
            {list.map((val, key) => {
                return <div className="row">{val.Tweet}</div>
            })}
        </div>
    </div>
);}
export default App;

上面的代码从我的 MySQL 数据库中无限获取数据,如何修改它以在一定间隔后获取数据?

【问题讨论】:

    标签: mysql reactjs express


    【解决方案1】:

    使用 setInterval

    useEffect(() => {
       setInterval(
          () => Axios.get('http://localhost:3001/getdata').then((response) => {
             setList(response.data)
          }),
          5000 // 5seconds
       )
    }, [])
    

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 2017-12-31
      • 2021-04-17
      • 2021-04-15
      • 2020-01-06
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多