记react 使用的问题

时间:2020-04-01
本文章向大家介绍记react 使用的问题,主要包括记react 使用的问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

问题描述: 1. react组件中,组件中正执行某个请求,请求未结束时卸载该组件,请求使用promise,此时promise状态为pedding,  组件componentWillUnMount已经执行;     2. 重载该组件后,请求的promise会继续执行,  

当时测试环境前提: TestComponent  父组件并未卸载

代码如下

class TestComponent extends Component {
    
    componentDidMount(){
        
    }
    
     componentWillUnMount(){
        console.log('unMount')
    }
    
     async request(){

         await startReqesut()

          this.props.callback && this.props.callback()
    }

}

加载 TestComponent后,比如点击执行 this.request();    startReqesut接口请求还未结束时, 卸载TestComponent组件,componentWillUnMount()已经执行

下次重载TestComponent时, 会执行 startRequest后面的代码 ,即 this.props.callback

卸载TestComponent后, js事件循环并未将startRequest 生成的微任务执行

原文地址:https://www.cnblogs.com/tangyuu/p/12615378.html