反应本机动画/恢复的内存泄漏

时间:2020-11-04 05:34:24

标签: javascript reactjs react-native memory-leaks expo

有关典型消息的一个问题

警告:无法在已卸载的组件上执行React状态更新。这是空操作,但它表明应用程序中发生内存泄漏。要修复,请取消使用useEffect清理功能中的所有订阅和异步任务

我正在分析应用程序的性能,并且注意到在解决了组件中的一些内存泄漏之后,我的应用程序的性能有了显着提高。

我正在实现的一个缓存渐进图像组件正面临一次内存泄漏,该组件使用Animated.timing()来更新动画状态值。

这样做:

   _isMounted = false; // Track the component mounted status

    state = {
        isLoading: this.props.withLoading,
        imageOpacity: new Animated.Value(0),
        thumbnailOpacity: new Animated.Value(0),
    };

    onLoadImage = () => {
        // Only update the state if the component is mounted
        if (!this._isMounted) return; // <------------------------ SOLVE THE MEMORY LEAK FOR isLoading update

        this.setState({ isLoading: false }, this.startLoadImageAnimation);
    };

    startLoadImageAnimation = () => {
        // Only update the state if the component is mounted
        if (!this._isMounted) return; // <----------- Seems to not being solving the possible memory leak with the animated stateful value updates

        const { withFade, onLoadImage } = this.props;
        const { imageOpacity } = this.state;

        if (withFade) {
           const { imageFadeDuration } = this.props;

           Animated.timing(imageOpacity, {
              toValue: 1,
              easing: Easing.linear,
              duration: imageFadeDuration,
              useNativeDriver: true,
           }).start();
     }

     onLoadImage();
  };

  
  componentDidMount() {
      this._isMounted = true;
  }

  componentWillUnmount() {
      this._isMounted = false;
  }

似乎无法解决动画状态值更新的内存泄漏问题...

有人遇到过类似的问题吗?

我有办法停止componentWillUnmount中的动画,但是我不确定这是否值得推荐。

谢谢。

0 个答案:

没有答案