Reactjs 在提取数据时,如果没有数据显示一个组件,并且如果数据在React web app中显示另一个组件,则呈现加载程序
我试图构建一个React组件,它遵循以下基本原则:Reactjs 在提取数据时,如果没有数据显示一个组件,并且如果数据在React web app中显示另一个组件,则呈现加载程序,reactjs,Reactjs,我试图构建一个React组件,它遵循以下基本原则: 在页面加载时,在提取数据时呈现加载程序组件 如果axios请求的结果中有数据,则渲染数据 如果axios请求的结果中没有数据(即没有要提取的数据),则呈现某个组件 我试图通过组合条件渲染来实现这一点。这取决于我的“fetchInProgress”状态,它在我的componentDidMount中设置为“true” 然而,就目前情况而言,加载程序会短暂出现(最多1秒),然后呈现一个空白屏幕 很明显,我的conditional有点问题,但我不知
- 在页面加载时,在提取数据时呈现加载程序组件
- 如果axios请求的结果中有数据,则渲染数据
- 如果axios请求的结果中没有数据(即没有要提取的数据),则呈现某个组件
class Breakdown extends Component {
constructor(props) {
super(props)
this.state = {
debts: [],
fetchInProgress: false
}
this.failedAccountCheck = this.failedAccountCheck.bind(this)
this.successfulAccountCheck = this.successfulAccountCheck.bind(this)
}
componentDidMount() {
this.setState({
fetchInProgress: true
})
axios.get("/api/fetchDebtCards")
.then((response) => {
this.setState({
debts: response.data,
fetchInProgress: false
})
})
.catch(e => {
console.log(e)
this.setState({
fetchInProgress: false
})
})
}
failedAccountCheck() {
return (
<div className="breakdown-analytics-container">
</div>
)
}
successfulAccountCheck() {
return (
<div className="breakdown-analytics-container">
</div>
)
}
render() {
return (
<div className="breakdown-flex">
{ this.state.fetchInProgress === true ? <Loader /> :
this.state.debts.length === 0 ? this.failedAccountCheck :this.successfulAccountCheck }
</div>
)
}
}
类细分扩展组件{
建造师(道具){
超级(道具)
此.state={
债务:[],
fetchInProgress:false
}
this.failedAccountCheck=this.failedAccountCheck.bind(this)
this.successfulcountcheck=this.successfulcountcheck.bind(this)
}
componentDidMount(){
这是我的国家({
fetchInProgress:对
})
axios.get(“/api/fetchDebtCards”)
。然后((响应)=>{
这是我的国家({
债务:回应。数据,
fetchInProgress:false
})
})
.catch(e=>{
控制台日志(e)
这是我的国家({
fetchInProgress:false
})
})
}
失败的帐户检查(){
返回(
)
}
成功的帐户检查(){
返回(
)
}
render(){
返回(
{this.state.fetchInProgress===true?:
this.state.deborts.length==0?this.failedAccountCheck:this.successfulAccountCheck}
)
}
}
有人能指出我哪里出了问题吗