初次使用React遇到的坑

414 阅读1分钟

定时器跳转页面

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

原因是组件挂载之后,设置了定时器,在callback进行了setState操作。当你切换路由是,组件已经被卸载,此时异步操作callback仍在操作,因此setState没有得到值。

内存泄露文问题,解决方案:

componentWillUnmount() {
    this.setState = (state,callback)=>{
        return;
    };
}