I am trying to get No list items only when there's nothing coming from the backend. Right now, onload, I get the loading spinner and No List items before I fetch the data.
So, I thought I would add a timeout to deal with this so that it will only show up after the fetching is done, and there are no items
getList() {
if(this.state.list.length != 0){
return (this.state.list.map(data => {
return <div data={data} key={data.id}/>
}))
}else{
return <div>No List items</div>
}
}
render() {
return (
<div>
<Spinner active={this.state.active} />
<div>{setTimeout(this.getList, 1000)}</div>
</div>
);
}
}
When i use this, I am getting numbers on the browser. The active state of spinner changes on componentDidMount to false
setTimeoutreturns an ID that can be passed toclearTimeout.setTimeoutdoesn't and cannot return the return value of the callback sincesetTimeoutdoesn't actually execute the function at the moment it is called. UsingsetTimeoutin your situation doesn't make sense.this.state.listbenullinitially, which would mean that the data has not been fetched from the server yet. Or, assuming thatthis.state.activeistruewhile the data is fetched andfalseonce it is fetched, you can just do{this.state.active ? null : this.getList()}.getListifthis.state.listisnull. We don't know the rest of your code and what you do withthis.state.listso we have to trust that you know how to apply the suggestion to your situation.