This question is an exact duplicate of:
I want the background of the view to be changed based on state , that comes from the API , I want the background of the view to be changed for 1 second whenever the state changes
Here is my code, This is what I tried to do
//*******************************************************
async componentDidMount() {
try {
setInterval(async () => {
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const blocks = await res.json();
if(this.state.a!==blocks.id){ //comparing with initial state which is null
this.setState({backgroundcolor:"#98fb98"})
} //I want this to happen for 1 second whenever the state changes
this.setState({
panelone: dataPanelone,
paneltwo: dataPaneltwo,
})
}, 2000);
} catch(e) {
console.log(e);
}
}