简体   繁体   English

无法在已卸载的组件上调用setState(或forceUpdate)。 react-image-gallery上的内存泄漏

[英]Can't call setState (or forceUpdate) on an unmounted component. Memory leak on react-image-gallery

I've been trying to implement the react-image-gallery v0.8.7 (0.8.8 has a bug ) from this npm package: https://github.com/xiaolin/react-image-gallery and integrated following the example as follows (I am developing a Meteor web app): 我一直在尝试从此npm包中实现react-image-gallery v0.8.7(0.8.8有一个bug ): https : //github.com/xiaolin/react-image-gallery并按照以下示例进行集成如下(我正在开发Meteor网络应用程序):

class MyGallery extends Component {
    constructor(props) {
    super(props);
    this.state = {
      mediaSrc: [],
      isFullScreen: false
    };
  }

  componentWillMount() {
    const mediaSrc = this.props.myObject.pictures.map((picture) => {
      return { original: picture, thumbnail: picture };
    });
    this.setState({ mediaSrc });
  }

  _onImageClick(event) {
    if (this.state.isFullScreen) {
      this._imageGallery.exitFullScreen();
      this.setState({ isFullScreen: false });
    } else {
      this._imageGallery.fullScreen();
      this.setState({ isFullScreen: true });
    }
  }

  render() {
    return (
      <div className="dish row">
        <figure className="center col-12" >
          <div className="dish__preview_container">
            <ImageGallery
              ref={i => this._imageGallery = i}
              items={this.state.mediaSrc}
              onClick={this._onImageClick.bind(this)}
              showFullscreenButton={false}
              showIndex
              showPlayButton={false}
              showThumbnails={false}
            />
         </div>
      );
  }
}

MyGallery.propTypes = {
  myObject: PropTypes.object.isRequired,
}

}

The object myObject contains the following value in the pictures array: 对象myObject在pictures数组中包含以下值:

[ 'https://media-cdn.tripadvisor.com/media/photo-s/05/6c/2b/9b/america-s-taco-shop.jpg',
  'https://www.cocinavital.mx/wp-content/uploads/2017/09/tostadas-de-tinga-de-pechuga-de-pollo-con-chipotle-video.jpg'
]

When rendering the ImageGallery is shown as expected, however when clicking on either the button aria-label="Previous Slide" or aria-label="Next Slide", doesn't show the respective image and throws the following exception on the developer tools console: 渲染ImageGallery时按预期显示,但是单击aria-label =“ Previous Slide”或aria-label =“ Next Slide”按钮时,不显示相应的图像,并且在开发人员工具上引发以下异常安慰:

Warning: Can't call setState (or forceUpdate) 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.
    in ImageGallery (created by MyGallery)
    in div (created by MyGallery)

Any suggestions for a solution, please? 有解决方案的建议吗?

Update: Had a reset of component state variables on the componenteWillUmnount method. 更新:在componenteWillUmnount方法上重置了组件状态变量。 Removed it, also tried with Meteor Reactive Dic t instead of component state variables. 删除它,也尝试使用Meteor Reactive Dic t代替组件状态变量。 The exception remains, though. 但是,例外仍然存在。

According to React Official Documentation , you should not call setState() in componentWillUnmount method because your component will never be re-rendered. 根据React Official Documentation ,您不应在componentWillUnmount方法中调用setState() ,因为您的组件将永远不会被重新渲染。 To date, I only use this method to remove event listeners added in componentDidMount() . 迄今为止,我仅使用此方法删除在componentDidMount()添加的事件侦听器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在已卸载的组件上调用setState(或forceUpdate)。 应对 - Can't call setState (or forceUpdate) on an unmounted component. React 无法在已卸载的组件上调用setState(或forceUpdate)。 这是空操作,但表示您的应用程序内存泄漏 - Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application 反应警告:无法在未安装的组件上调用 setState(或 forceUpdate) - React Warning: Can't call setState (or forceUpdate) on an unmounted component 警告:无法在React Native中的已卸载组件上调用setState(或forceUpdate) - Warning: Can't call setState (or forceUpdate) on an unmounted component in React Native 反应-警告:无法在未安装的组件上调用setState(或forceUpdate) - React - Warning: Can't call setState (or forceUpdate) on an unmounted component 无法在卸载的组件上调用setState(或forceUpdate) - Can't call setState (or forceUpdate) on an unmounted component 警告:无法在卸载的组件上调用setState(或forceUpdate) - Warning: Can't call setState (or forceUpdate) on an unmounted component 如何解决:无法在 React-Native 中的未挂载组件警告上调用 setState(或 forceUpdate)? - How to solve: Can't call setState(or forceUpdate) on an unmounted component warning in React-Native? 无法对未安装的组件执行反应 state 更新。 这是一个空操作,但它表示 memory 泄漏 - can't perform a react state update on an unmounted component. this is a no-op but it indicates a memory leak 无法使用componentDidMount从API提取数据:无法在已卸载的组件上调用setState(或forceUpdate) - Cannot fetch data from an API using componentDidMount: Can't call setState (or forceUpdate) on an unmounted component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM