reactjs - 加载动画未在 React 中渲染
问题描述
我正在使用 Spotify API 开发一个 react-js 项目,该项目将在登录后显示用户特定的统计信息。
当前的过程是用户进入主页,点击登录,spotify 授权他们,我渲染登录屏幕,其中很多 API 调用和计算在 componentDidMount() 中完成。
我有一个 state 属性,itemsLoaded
默认情况下它是 false,但是一旦计算完成,我在我的componentDidMount()
. 在我的render()
函数中,我想在itemsLoaded
为 false 时呈现加载页面,这曾经完美地工作,但大约一天前停止工作,当时我对它之前的辅助方法进行了一些编辑,导致进行了更多的 API 调用。我不确定为什么加载器不工作,并在那里放置一个 console.log,它在页面加载时正在打印,但我正在导入的加载组件不再显示。任何关于该问题的建议或见解将不胜感激,谢谢!
- 这不是确切的代码,但我输入了所有相关的部分
constructor(props) {
super(props);
history.push('/');
this.state = {
userData: {},
recievedError: false,
itemsLoaded: false
};
}
async componentDidMount(){
try{
//bunch of helper methods and calculations and API calls
this.setState({itemsLoaded:true},this.props.onChangeParentStyle(true,true,1));
}
catch(e){
//redirect to home page
}
}
render() {
const dataLoaded = this.state.itemsLoaded;
if (this.state.recievedError) {
console.log(this.state.errorMsg);
alert('Error: Spotify is having an issue sending us information about your music selection.')
return(<App></App>);
}
else if (!dataLoaded) {
console.log('loading :/')
return (<Loading></Loading>)
} else {
return(
<div id="pagepiling">
{ this.intro() }
{ this.thankYou() }
</div>
);
}
解决方案
推荐阅读
- c++ - 有没有办法从 C++ 中的函数返回自定义结构?
- javascript - 币安链钱包扩展问题
- vaadin - Vaadin Flow:如何将类名添加到模板
- python - 在 python 中显示来自 mysql 的数据时,时间和日期列看起来几乎不可读。有没有办法以日期和时间格式显示它?
- twitter - Tweepy 机器人正在回复所有转发或回复特定用户推文的人
- python - 如何在 Python 中制作常规计时器?
- kubernetes - 如何创建 kubernetes 作业规范来运行 perl 脚本?
- vue.js - 在 Nuxt 中更改路线后如何不向下滚动页面?
- php - Laravel 项目中的“npm run hot”抛出无效配置错误
- mysql - mysql 创建表查询执行时间过长