Created
December 13, 2018 05:07
-
-
Save yano3nora/e506210e7cd5449f4ec8ce526fff6ff8 to your computer and use it in GitHub Desktop.
[react: Image onError] Behavior on error of image tag. #react #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<img | |
src={this.props.img_url} | |
alt={this.props.img_alt} | |
onError={(e) => { | |
e.target.onerror = null | |
e.target.src = '/img/error-image.png' | |
}} | |
/> |
This is amazing man.
This made TypeScript a bit grumpy: TS2339: Property 'src' does not exist on type 'EventTarget'.
even though it works fine.
currentTarget
will satisfy TS:
e.currentTarget.src = '/img/error-image.png'
how can we do this in React Native? am able to do using useState with function, but problem whenever it rerender it change the all the other images
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with typescript is it also possible?