您可以
isMounted在此处使用React模式来避免内存泄漏。
在您的构造函数中:
constructor(props) { super(props); this._isMounted = false;// rest of your pre}componentDidMount() { this._isMounted = true; this._isMounted && this.getImage(this.props.item.image);}
在你的
componentWillUnmount
componentWillUnmount() { this._isMounted = false;}在你里面
getImage()
async getImage(img) { let imgUri = await Amplify.Storage.get(img) let uri = await CacheManager.get(imgUri).getPath() this._isMounted && this.setState({ image: { uri }, ready: true })}基于可取消承诺模式的推荐使用 Axios的 方法。因此,您可以在卸载组件的同时取消任何网络呼叫
cancelTokensubscription。这是Axios取消的资源



