admin管理员组文章数量:1289425
I have a react app where I load images using another ponent called load-image
.
Now I pass src
to load-image
, it shows a nice loader till image loads, and when its loaded, it shows a nice animation of image.
The problem arises here. I opened the app page, All images start loading. Now user goes to another page, the images are still loading. I can see them loading in the console. Now I get this error in console.
Warning: setState(...): Can only update a mounted or mounting ponent. This usually means you called setState() on an unmounted ponent. This is a no-op. Please check the code for the undefined ponent.
Here is my code.
export default class LoadImage extends React.Component {
constructor() {
super();
this.state = {
isLoaded: false,
isMounted: false,
};
this.onImageLoad = this.onImageLoad.bind(this);
}
ponentDidMount() {
const imgSrc = this.props.imageSrc;
const img = new window.Image();
img.onload = this.onImageLoad;
img.src = imgSrc;
this.setState({
isMounted: true,
});
}
ponentWillUnmount() {
this.setState({
isMounted: false,
});
}
onImageLoad() {
const self = this;
if (self.state.isMounted === true) {
self.setState({
isLoaded: true,
});
}
}
render() {
const self = this;
const imageClasses = classNames({
'image-loaded': self.state.isLoaded,
'image': true,
});
const imgStyle = {
backgroundImage: 'url("' + self.props.imageSrc + '")',
backgroundSize: 'cover',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
width: 'inherit',
height: 'inherit',
};
return (
<div className="image-loader">
<div style={ imgStyle } className={ imageClasses }>
</div>
</div>
);
}
}
How can i cancel older request so that they don't update state after unmounting. I am already using state to check if ponent is mounted or not. Thanks.
I have a react app where I load images using another ponent called load-image
.
Now I pass src
to load-image
, it shows a nice loader till image loads, and when its loaded, it shows a nice animation of image.
The problem arises here. I opened the app page, All images start loading. Now user goes to another page, the images are still loading. I can see them loading in the console. Now I get this error in console.
Warning: setState(...): Can only update a mounted or mounting ponent. This usually means you called setState() on an unmounted ponent. This is a no-op. Please check the code for the undefined ponent.
Here is my code.
export default class LoadImage extends React.Component {
constructor() {
super();
this.state = {
isLoaded: false,
isMounted: false,
};
this.onImageLoad = this.onImageLoad.bind(this);
}
ponentDidMount() {
const imgSrc = this.props.imageSrc;
const img = new window.Image();
img.onload = this.onImageLoad;
img.src = imgSrc;
this.setState({
isMounted: true,
});
}
ponentWillUnmount() {
this.setState({
isMounted: false,
});
}
onImageLoad() {
const self = this;
if (self.state.isMounted === true) {
self.setState({
isLoaded: true,
});
}
}
render() {
const self = this;
const imageClasses = classNames({
'image-loaded': self.state.isLoaded,
'image': true,
});
const imgStyle = {
backgroundImage: 'url("' + self.props.imageSrc + '")',
backgroundSize: 'cover',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
width: 'inherit',
height: 'inherit',
};
return (
<div className="image-loader">
<div style={ imgStyle } className={ imageClasses }>
</div>
</div>
);
}
}
How can i cancel older request so that they don't update state after unmounting. I am already using state to check if ponent is mounted or not. Thanks.
Share Improve this question edited Jan 6, 2016 at 1:50 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Dec 9, 2015 at 18:42 Ateev ChopraAteev Chopra 1,0263 gold badges16 silver badges32 bronze badges 2- goo.gl/L0CHIX – Oleksandr T. Commented Dec 9, 2015 at 18:44
- How do i check if ponent is mounted or not ? I'm Following this: jaketrent./post/set-state-in-callbacks-in-react – Ateev Chopra Commented Dec 9, 2015 at 18:45
2 Answers
Reset to default 10You can change the callback while the image is still loading. In ponentWillUnmount
, set this.img.onload
to a function that does nothing.
ponentDidMount() {
const imgSrc = this.props.imageSrc;
this.img = new window.Image();
this.img.src = imgSrc;
this.img.onload = this.onImageLoad;
}
ponentWillUnmount() {
if ( ! this.img ) {
return;
}
this.img.onload = function(){};
delete this.img;
}
Source: https://github./Automattic/wp-calypso/blob/master/client/ponents/image-preloader/index.jsx
No need for isMounted() if you take this approach.
Don't call setState
in ponentWillUnmount
. If the ponent is about to unmount, there's no point altering its state since it's about to be removed from the DOM.
本文标签: javascriptCancel Image onload when component unmountsStack Overflow
版权声明:本文标题:javascript - Cancel Image onload when component unmounts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741477401a2380965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论