[英]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.