首页 > 解决方案 > 加载动画未在 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>
            );
        }

标签: reactjsapidomrenderloading

解决方案


推荐阅读