componentDidMount()中的setState()在APK中引发错误

我目前正在使用Expo 35(或React Native 0.59)进行项目开发

使用expo start在IOS模拟器和ADV中进行测试时,我的代码可以正常工作。

但是,当在APK(expo build:android)中进行测试时,它会以某种方式引发错误并自行自行关闭。

这是我的代码。

  componentDidmount() {

        Promise.all([
            4~5 Axios requests...             
        ])
            .then(() => {

                this.setState({
                    ...
                    loaded: true,...
                });
            })

    }

有解决此问题的主意吗?

huang118118bing 回答:componentDidMount()中的setState()在APK中引发错误

componentDidMount在渲染组件之前启动,setState将重新渲染组件,并会触发额外的渲染,但这会在应用更新屏幕之前发生,这是错误的。

您可以改为执行以下操作:

_execute = ()=>{
   Promise.all([
            4~5 Axios requests...             
        ])
            .then(() => {

                this.setState({
                    ...
                    loaded: true,...
                });
            })
}

componentDidMount() {
    this._execute()
}
,
       _execute = ()=>{
       Promise.all([
        4~5 Axios requests...             
       ])
       .catch(e=>console.warn(e)) 
        .then(() => {

            this.setState({
                ...
                loaded: true,...
            });
        })
}

componentDidMount() {
this._execute()
}

添加捕获可以发现错误

本文链接:https://www.f2er.com/2991339.html

大家都在问