React Native,动态显示图像



我目前正在处理一个页面,希望在该页面上显示文件夹中的所有图像。我尝试了以下代码:

return (
<View>
{images.map(r => 
<Image source={require('../assets/image/' +r+'.png')} />
)}    
</View>
);

这是我得到的结果:

Error: pages/etc.js:Invalid call at line: require('../assets/image/' + r + '.png')

我应该怎么做才能解决这个问题?谢谢

React Native中的require在这种特定情况下不适用于动态路由(带有变量的路由(。应该需要一个带有pur字符串路径的图像。你想要的可以用这样的东西来做:

//.....
const images =[require('../assets/image/1.png'),require('../assets/image/2.png')]
//.....
return (
<View>
{images.map(i => 
<Image source={{uri:i}} />
)}    
</View>
);

如果没有像数组这样的本地附加数据库,你就无法解决它,这也需要

最新更新