【问题标题】:Implementing nested fetch API calls with setInterval for dashboard: fetch()+React+Typesctipt使用 setInterval 为仪表板实现嵌套的 fetch API 调用:fetch()+React+Typescript
【发布时间】:2019-05-02 20:27:33
【问题描述】:

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

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

有什么更好的方法?

我们将不胜感激

 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
         />
     )   
  }
}

【问题讨论】:

    标签: reactjs promise fetch setstate react-table


    【解决方案1】:
    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
                />
            )
        }
    }
    

    【讨论】:

    • 我只希望第二个调用在 setInterval 之下。再次感谢
    • 不需要interval,一旦取到数据,timeout会再次调用feching,2000ms后进入循环
    • @ Barbu Barbu ,另外,如果我有多个表遵循相同的 fetch 调用序列,我该如何实现所有这些?像这样,如何让所有表格每 2 秒刷新一次?
    • @velsonjr 创建一个 HOC,它将一个 url(用于获取数据的 url)作为道具,然后自动执行此操作,以及您可能想要的任何其他特性/功能。
    • @ManavM 我有点新反应,可以分享一个sn-p吗?
    猜你喜欢
    • 2016-08-30
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多