使用仪表板的 setInterval 实现嵌套 fetch API 调用:fetch()+React+Typescript

Implementing nested fetch API calls with setInterval for dashboard: fetch()+React+Typesctipt

我正在尝试使用 React table 调出仪表板页面。我希望每 2 seconds.Also 刷新一次 table 我有两个 fetch api 调用,其中第一个的结果作为参数传递给第二个。此外,第二次获取调用必须每两秒设置一次图表数据。

此外,如果有多个 table 遵循此类提取调用的相同实现,我该怎么做?

更好的方法是什么?

不胜感激

 import * as React from 'react';
 import ReactTable from 'react-table';
 import 'react-table/react-table.css';

 interface IState {
    data: any[];
 }

 export default class Dashboard extends React.Component<{},IState> {

   constructor(props: any) {
     super(props);
     this.state = {
       data: [],
   }

   componentDidMount()
   { 
       const url="/api/query/fetch";    

        var result = fetch(url, {
            method: 'post',
            body : JSON.stringify({"id":"abc"),
            headers: {
                     'Content-Type': 'application/json'
                    },
           }).then(function(response) {
        return response.json();

       }).then(function(data) {

        return fetch('api/query/fetch' + data.id); // want this call to be under setInterval such that chart data refreshes every 2 seconds, s uch that table data refreshes every 2 seconds
      })
      .then(function(response) {
           return response.json();
      })
      .catch(function(error) {
          console.log('Request failed', error)
      })
    result.then(function(r) {
         this.setState({data:r});
    });
  }     

  render(){
     return(
        <ReactTable 
           data={this.state.data} 
           columns={columns} //column config object
         />
     )   
  }
}
import * as React from 'react';
import ReactTable from 'react-table';
import 'react-table/react-table.css';

interface IState {
    data: any[];
    id: any;
}

export default class Dashboard extends React.Component<{}, IState> {

    constructor(props: any) {
        super(props);
        this.state = {
            data: [],
            id: null
        }
    }

    componentDidMount() {
        const url = "/api/query/fetch";

        fetch(
            url, 
            {
                method: 'post',
                body: JSON.stringify({ "id": "abc"}),
                headers: {
                    'Content-Type': 'application/json'
                }
            }
        ).then(function (response) {
            return response.json();
        }).then(function (data) {
            this.setState({
                id: data.id
            }, () => this.fetchData())
        });
    }

    fetchData() {
        fetch('api/query/fetch' + this.state.id)
        .then(function (response) {
            return response.json();
        })
        .then(function (r) {
            this.setState({ data: r });
            setTimeout(() => {
                this.fetchData();
            }, 2000);
        })
        .catch(function (error) {
            console.log('Request failed', error)
        })
    }

    render(){
        return (
            <ReactTable
                data={this.state.data}
                columns={columns} //column config object
            />
        )
    }
}