React-Native中的图像源错误:需要正好1个文本字符串

React-Native中的图像源错误:需要正好1个文本字符串,react-native,require,React Native,Require,嗨,我在react native的本地路径中有iumages,我正在尝试提供路径作为 <ListItem roundAvatar avatar={<Avatar rounded source={require( '../../../public/images/'+props.item.ImageName+'.png')} title={props.item.cat_name} />} onPres

嗨,我在react native的本地路径中有iumages,我正在尝试提供路径作为

 <ListItem
    roundAvatar
    avatar={<Avatar
        rounded
        source={require( '../../../public/images/'+props.item.ImageName+'.png')}
        title={props.item.cat_name}
    />}
    onPress={() => props.subscreen(props.item._id)}
    key={props.item._id}
    title={props.item.cat_name}
/>
试试这个:

require(`../../../public/images/${props.item.ImageName}.png`)

引号的类型不同。

在require中不能使用变量,只能使用字符串,不能使用变量。 您需要这样做:

avatar={<Avatar
        rounded
        source={require( '../../../public/images/nameOfPic.png')}
        title={props.item.cat_name}
    />}
avatar={}
如果你想选择一个图像,你可以这样做

arrayImgs = [require('../../anyName1.jpg'), require('../../anyName2.jpg')];
...
 <ListItem
    roundAvatar
    avatar={<Avatar
        rounded
        source={isTrue?arrayImgs[0]:arrayImgs[1]}
        title={props.item.cat_name}
    />}
    onPress={() => props.subscreen(props.item._id)}
    key={props.item._id}
    title={props.item.cat_name}
/>
arrayImgs=[require('../../anyName1.jpg')、require('../../anyName2.jpg');
...
道具子屏幕(道具项。\u id)}
key={props.item.\u id}
title={props.item.cat_name}
/>
编辑:

您可以对本地图像使用URI

let imagePath = 'file:///storage/emulated/0/Pictures/'+imageName+'.png'
...
<Image
              style={{width: '100%', height: 400}}
              resizeMode='contain'
              source={{imagePath}}
            />
让imagePath='1file:///storage/emulated/0/Pictures/“+imageName+”.png”
...

这是Android中文件夹的默认路径,请尝试使用“Pictures”文件夹中的任何图像。

我使用在100行上动态显示100个图像,而不是浏览两个图像。我如何实现这一点?因此,如果您有这么多图像,您需要使用“uri”,请使用path='file://'+pathToResources+'/'+href+.png';source={{uri:path}}您不需要转换,但您需要下载图像或将图像放入手机中。您需要知道文件的路径,我将展示一个示例,等等