3 回答

TA贡献1784条经验 获得超2个赞
exports class Loading extends Component {
state = {
sample: [],
loading: true
};
componentDidMount() {
this.setState({sample: sample, loading: false});
}
render() {
return (
<div>
{this.state.loading ?
<Spinner /> : <Data />
}
</div>
)
}
}
检查此方法。

TA贡献1830条经验 获得超9个赞
您可以将状态设置为在发出请求之前,然后设置为返回响应之后(对于成功和失败的响应)。loadingtrueloadingfalse
async componentDidMount() {
this.setState({
loading: true
}):
this.sampleService
.getAllSample()
.then((sample) => {
this.setState({
sample,
loading: false,
)};
})
.catch((err) => {
console.log(err);
this.setState({
loading: false,
)};
});
}

TA贡献1802条经验 获得超10个赞
您可以将其设置为函数,并在 AJAX 调用之前设置为 ,并将其设置为在接收数据后。您还可以将其设置为在错误情况下async/awaitloadingtruefalsefalse
试试这样的东西
async componentDidMount() {
this.setState({loading: true});
try
{
const res = await this.sampleService.getAllSample();
this.setState({sample: res, loading: false);
}
catch(err)
{
this.setState({loading:false});
console.log(err);
}
}
添加回答
举报