0

To send entire require('./images/logo.png') as a prop i have to create boolean to check if the image is from web or is being passed from a local folder. How to create this kind of boolean?

type ImageProps = {
    imageName: string;
    fromWeb: boolean;
};

export function CustomImage({ imageName, fromWeb }: ImageProps) {
    return (
        <View>
            <Image style={styles.image} source={imageName} />
        </View>
    );
}

File from outside the component

function PassedImages() {
        return (
            <View>
             <CustomImage fromWeb={false} imageName={require('./images/logo.png')}/>
            </View>
        );
    }

1 Answers1

0

No need to check the origin of the image. Image handles that and works all options below.

const assetImage = require('@expo/snack-static/react-native-logo.png');
const networkImage = 'https://reactnative.dev/img/tiny_logo.png';

<Image
  style={styles.logo}
  source={{uri: assetImage}}
/>
<Image
  style={styles.logo}
  source={assetImage}
/>
<Image
  style={styles.logo}
  source={{
    uri: networkImage,
  }}
/>
<Image
  style={styles.logo}
  source={networkImage}
/>
user18309290
  • 5,777
  • 2
  • 4
  • 22