React-Native check image url

12,544

Solution 1

you are not returning anything on error response.

add .catch and return a component or null in that. for eg

function checkImageURL(URL) {
  fetch(URL)
    .then((res) => {
      if (res.status == 404) {
        return <Image source={require('./Images/default.png')} />;
      } else {
        return <Image source={{ uri: `${url}` }} />;
      }
    })
    .catch((err) => {
      return <Image source={require('./Images/default.png')} />;
    });
}

Solution 2

Actually, you don't need to check if the image response status it's not 404 to be able to display it. You just need to fetch your data and store it in your state, then check if the image can be displayed like the example below.

I am not exactly what are you trying to build but in my opinion, you should never make a request to fetch an image.

There is a defaultSource prop now.

<Image
  source={{ uri: 'https://example.com/image.png' }}
  style={{ height: 200, width: 200 }}
  defaultSource={defaultImg}
/>

Check the snack https://snack.expo.io/@abranhe/defaultimage

Solution 3

Although the selected answer solves the problem in question, the overarching problem here I notice is the way the OP is handling an image uri that returns a 404. If your intention is to have a fallback image in the case that an image's uri is invalid for whatever reason (any 400ish response), I'd recommend leveraging the onError <Image/> prop.

I have an <Avatar/> component that uses this prop:

import { Image, ImageStyle } from 'react-native'
import React, { useState } from 'react'
import { avatarPlaceholder } from '../../assets/images'

interface Props {
  avatar: string;
  style: ImageStyle;
}

const Avatar = React.memo((props: Props) => {
  const [valid, setValid] = useState(true)
  const { avatar, style } = props

  return (
    <Image
      onError={() => setValid(false)}
      style={[{
        resizeMode: "cover",
        borderRadius: 1000,
      }, style]}
      source={{ uri: valid ? avatar : avatarPlaceholder }}
    />
  )
})

export default Avatar

Happy coding!

Share:
12,544

Related videos on Youtube

jrocc
Author by

jrocc

Updated on June 04, 2022

Comments

  • jrocc
    jrocc almost 2 years

    I am trying to check if the url of an image is 404 or not. The problem is that the function is returning undefined. Why is this happening?

    If I console.log the status of the res it does show 404 so the if statement is being executed.

    function checkImageURL(url){
     fetch(url)
        .then(res => {
        if(res.status == 404){
          console.log(res.status)
          return <Image source={require('./Images/default.png')}/>
        }else{
          return <Image source={{uri: `${url}`}}  />
       }
     })
    }
    
  • jrocc
    jrocc almost 5 years
    The catch statement did work I think because the fetch is getting an error its just getting a 404 status response.
  • Piyush Abhishek Singh
    Piyush Abhishek Singh almost 5 years
    So it solved your problem . Then please accept my answer.
  • The1993
    The1993 about 4 years
    This does not work. If your some-url path is not a valid image, it will show empty image instead of the defaultImg
  • instanceof
    instanceof about 3 years
    Agreed, this does not work, exactly as The1993 said.
  • Abraham Calf
    Abraham Calf about 3 years
    I have updated the answer and it should be working fine now
  • Admin
    Admin almost 2 years
    Ahh yes this is a huge life saver. I was trying to use the on error to change the state but I was often mapping over multiple avatars so one 404 would set them all to false. I knew there was a better way.