question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

clear `src` attribute on recycled `<img>` nodes

See original GitHub issue

Here’s a codepen with the problem: when the src attribute of an image is changed, the old picture will keep showing while the new picture is loading. This behavior persists even if the image is removed from the DOM. When combined with Preact’s node-caching, this can get crazy, with old pictures showing up again in unrelated places.

Clearing the image with .removeAttribute('src') should solve the issue. I’ll plan on putting together a pull request here shorty.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
josculcommented, Feb 25, 2021

Had the same problem. Solved it by adding the key={src} to my image component and it worked. Got the idea here: https://stackoverflow.com/questions/50690956/react-img-not-immediately-changing-when-src-changes-how-can-i-fix-this

2reactions
jackmoorecommented, May 30, 2018

I’ve also been running into this problem with old images showing while new images load, which is not a trivial amount of time. The old images may be visible for seconds.

I tried using .removeAttribute('src') on componentWillUnmount, which did work to start each image on a clean slate but there is some sort of race condition there because sometimes the new image would end up without an src attribute. It was unworkable.

I settled on creating this component as a replacement for <img> elements, which seemed to provide both a clean slate and resolve the .removeAttribute('src') race condition:

export default class extends React.Component {
	state = {src: null};

	componentDidMount() {
		this.setState({src: this.props.src});
	}

	componentWillReceiveProps(nextProps) {
		if (nextProps.src !== this.props.src) {
			this.setState({src: nextProps.src});
		}
	}

	render() {
		return <img {...this.props} src={this.state.src}/>;
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

clear src attribute on recycled <img> nodes #351 - GitHub
Here's a codepen with the problem: when the src attribute of an image is changed, the old picture will keep showing while the...
Read more >
Clear img src Attribute Using JavaScript - Linux Hint
Approach 1: Clear img src Attribute in JavaScript Using removeAttribute() Method · Specify the stated image having the specified “id” and the “src”...
Read more >
How can I prevent memory leaks when removing images in ...
Remove image src attribute before removing the image from the DOM; Set image src to " " before removing the image from the...
Read more >
WebView - Android Developers
Tell view hierarchy that the global view attributes need to be re-evaluated. void, removeAllViews(). Call this method to remove all child views from...
Read more >
Persistent Volumes | Kubernetes
When a user is done with their volume, they can delete the PVC objects ... Currently, volumes can either be Retained, Recycled, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found