How to call an API every minute for a Dashboard in REACT

后端 未结 4 1537
一整个雨季
一整个雨季 2021-02-04 03:55

I\'ve made a dashboard in React. It has no active updating, no buttons, fields or drop-downs. It will be deployed on a wall TV for viewing. All panels (9 total) are updated thro

4条回答
  •  情话喂你
    2021-02-04 04:30

    In order to use await, the function directly enclosing it needs to be async. According to you if you want to use setInterval inside componentDidMount, adding async to the inner function will solve the issue. Here is the code,

     async componentDidMount() {
              try {
                setInterval(async () => {
                  const res = await fetch('https://api.apijson.com/...');
                  const blocks = await res.json();
                  const dataPanelone = blocks.panelone;
                  const dataPaneltwo = blocks.paneltwo;
    
                  this.setState({
                    panelone: dataPanelone,
                    paneltwo: dataPaneltwo,
                  })
                }, 30000);
              } catch(e) {
                console.log(e);
              }
        }
    

    Also instead of using setInterval globally, you should consider using react-timer-mixin. https://facebook.github.io/react-native/docs/timers.html#timermixin

提交回复
热议问题