React Native Image how to use onError

10,006

This is an SyntheticEvent, as specified in the Image doc here, you can try following code

onError=({ nativeEvent: {error} }) => console.log(error)

Hope this will help.

Share:
10,006
tjkind
Author by

tjkind

I like to code and bake cupcakes

Updated on August 26, 2022

Comments

  • tjkind
    tjkind over 1 year

    Hi I'm currently trying to load several network images and they often fail to load on android. I'm trying to use onError to diagnose why but can't seem to get it to work.

    I've tried using it as

    onError={(e) => console.log(e}
    

    and

    onError(error){
      console.log(error)
    }
    
    onError={ this.onError.bind(this) }
    

    but both of these result in this object

    Proxy [[Handler]]: Object [[Target]]: SyntheticEvent [[IsRevoked]]: false

    which, as best as I can tell, is the error event and doesn't contain anything useful.

    My question is how do I use onError to diagnose why my network images are failing? but if you have any ideas about why I'll gladly take those too.

  • tjkind
    tjkind about 6 years
    Thanks for your help, I did not realise that was what the docs were saying. It's no longer giving me the object but the error is just 'undefined' and it looks like the native event is an empty object
  • Prasun
    Prasun about 6 years
    Hi @tjkind, my bad, I had put extra bracket, I have edited the answers, earlier it was {{ nativeEvent: {error} }}, but correct one is { nativeEvent: {error} }, please try agin, it should work. Sorry for the extra bracket.
  • tjkind
    tjkind about 6 years
    I did actually catch that. I'm currently using this onError={(e) => console.log(e.nativeEvent.error)} and it prints out undefined. But I've been told to move on so ¯_(ツ)_/¯
  • Noitidart
    Noitidart about 6 years
    nativeEvent is an empty object for me also. Are you sure this works for your Prasun?
  • Prasun
    Prasun about 6 years
    Yes, I was able to catch error using the said approach, take a look at this stackoverflow.com/questions/47254383/…. BTW which version of RN you are using?
  • Prasun
    Prasun about 6 years
    I just tested using the code <Image onError={({ nativeEvent: {error} }) => console.log(error) } style={{height: 200, width: 200}} source={{uri:'https://randomuser.me/api/portraits/women/2ww.‌​jpg'}}/> in RN-52, I was able to see the error log.
  • tjkind
    tjkind almost 6 years
    After several months of casual experimentation I've found that if the error is on ios there is an error message (sometimes not a useful one) but that on android it's always an empty object