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 through the API call. The initial call (seen below) works, and all JSON data is fetched and the dashboard is initially updated.
BOTTOM LINE PROBLEM: I need to call the API every 30 sec to 1 minute after the initial call to check for updates.
I have attempted “setInterval” inside the componentDidMount() as suggested by people on here answering others’ questions and I get an error “await is a reserved word”. I’ve read about forceUpdate() which seems logical for my use case given what the facebook/react page says about it. But, I’ve read on here as well to stay away from that…
The code below is a light version of the code I’m using. I’ve removed many of the components and imports for brevity’s sake. Any help would be greatly appreciated.
import React, { Component } from 'react';
import Panelone from './Components/Panelone';
import Paneltwo from './Components/Paneltwo';
class App extends Component {
state = {
panelone: [],
paneltwo: []
}
async componentDidMount() {
try {
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,
})
} catch(e) {
console.log(e);
}
render () {
return (
<div className="App">
<div className="wrapper">
<Panelone panelone={this.state} />
<Paneltwo paneltwo={this.state} />
</div>
</div>
);
}
}
export default App;
7
Answers
Move the data fetch logic into a seperate function and invoke that function using
setInterval
incomponentDidMount
method as shown below.Below is a working example
https://codesandbox.io/s/qvzj6005w
In order to use
await
, the function directly enclosing it needs to beasync
. According to you if you want to usesetInterval
insidecomponentDidMount
, adding async to the inner function will solve the issue. Here is the code,Also instead of using setInterval globally, you should consider using react-timer-mixin. https://facebook.github.io/react-native/docs/timers.html#timermixin
I figured I’d chime in with a slightly revised approach that uses recursion via a setTimeout call within the function block. Works the same…maybe slightly cleaner to have the function call itself from within, instead of doing this elsewhere in your code?
This article explains the reasoning in a bit more depth…but I’ve been using this approach for several dashboards at work – does the job!
Would look something like this:
Sorry if the formatting got a little off. Haven’t tried this with Hooks yet but I think you’d have a similar implementation in a useEffect call? Has anyone done that yet?
I have seen around a lot of complications about this. No need to have it in the lifecycles or in state or promisses.
In here, the service api is just a simple axios api call
This is my full implementation as I use it with context api(omitting some private code).
In my case I just care about the status response in the api since I know what I need to change. But the api can be really anything you need for/from data-wise.’
Answer
You can create a function for the componentDidMount code.
then make a componentDidUpdate
For those looking for functional components. You can update the state every n time by creating a setInterval and calling this in the
useEffect
hook. Finally call theclearInterval
method in the clean up functionYou also need to clear this interval, else it will keep on calling the API. To clear the interval, you need to have a reference and then you can clear it on
ComponentWillUnmount
.You can use
useRef
hook with your class component for this purpose.Functional Component Approach