React Native警告-取消componentWillUnmount中的所有订阅和异步任务

时间:2019-06-28 12:32:28

标签: reactjs react-native fetch

由于我的应用正在从API提取图像并按预期呈现结果。但是显示此警告对这个项目来说并不完整,给出的答案还不能解决我的问题。

此外,使用AbortController无法解决将信号作为参数传递给fetch调用并在AbortController.abort()中使用componentWillUnmount的问题

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.

代码:

componentDidMount() {
    this.getImage(Flikr_URL);
  }

  getImage(url) {
    fetch(url)
      .then(response => response.json())
      .then(responseJson =>
        this.setState({
          imageData: responseJson.photos.photo,
          loading: false
        })
      )
      .catch(err => {
        console.log(err);
        throw error;
      });
  }

  componentWillUnmount() {
    this.getImage();
  }

有人建议真的可以挽救我的生活。

0 个答案:

没有答案
相关问题