Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native React本机映像组件,需要未知模块_React Native - Fatal编程技术网

React native React本机映像组件,需要未知模块

React native React本机映像组件,需要未知模块,react-native,React Native,这些文档清楚地表明,您无法执行任何动态操作,需要: // GOOD <Image source={require('./my-icon.png')} /> // BAD var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive'; <Image source={require('./' + icon + '.png')} /> // GOOD var icon = this.props.ac

这些文档清楚地表明,您无法执行任何动态操作,需要:

// GOOD
<Image source={require('./my-icon.png')} />

// BAD
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
<Image source={require('./' + icon + '.png')} />

// GOOD
var icon = this.props.active ? require('./my-icon-active.png') : require('./my-icon-inactive.png');
<Image source={icon} />
这里有什么解决方法吗?它似乎仍然需要完全相同的字符串,它只是不起作用

您需要执行以下操作:

let imagePath = require("../img/" + row.number + ".png")
<Image style={styles.thumb} source={imagePath} />
let imagePath=require(“../img/”+row.number+“.png”)

好吧,我想出来了,我想我会发布一个答案,以防将来有人遇到这种麻烦

答案是,您需要明确要求所有图像,否则它们将不会被捆绑,因此出现未知模块错误

解决方案: 在另一个文件中,我创建并导出了一个由数字设置关键帧的对象,其值为require(../img/x.png),其中x是关键帧。 然后,在ListView的renderRow方法中,我通过使用row.number键入对象,将图像从对象中拉出

let imagePath = "../img/" + row.number + ".png";  // row.number being 12
<Image style={styles.thumb} source={require(imagePath)} />
Requiring unknown module "../img/12.png"
let imagePath = require("../img/" + row.number + ".png")
<Image style={styles.thumb} source={imagePath} />