[英]React Native image not showing when try to load location from json file
This is a json file that stores a title and a image location for every elements. 这是一个json文件,其中存储每个元素的标题和图像位置。
[ {"key": "a", "title": "Bangladesh", "image": "require('./img/bangladesh.jpg')"}, {"key": "b", "title": "Sports", "image": "require('./img/sports.jpg')"}, {"key": "c", "title": "Politics", "image": "require('./img/politics.jpg')"}, {"key": "d", "title": "Entertainment", "image": "require('./img/entertainment.png')"}, {"key": "e", "title": "Economics", "image": "require('./img/economics.jpg')"}, {"key": "f", "title": "Technology", "image": "require('./img/technology.jpg')"}, {"key": "g", "title": "Others", "image": "require('./img/m.jpg')"}, ]
And Now I want to show all image and title using a Flatlist. 现在,我想使用平面列表显示所有图像和标题。 Title shows perfectly but image not shows.
标题显示完美,但图像未显示。
<FlatList horizontal= {true} data={newsCategory} renderItem={({item}) => ( <TouchableOpacity style={styles.option} > <Image style={styles.imgButton} source={item.image} /> <Text style={styles.buttonText}>{item.title}</Text> </TouchableOpacity> ) } />
I check that image location comes perfectly from json file but not shows the image. 我检查图像位置是否完全来自json文件,但未显示图像。 Don't understand why this occurs.
不明白为什么会这样。
remove string from require. 从require中删除字符串。
change this 改变这个
[
{"key": "a", "title": "Bangladesh", "image": "require('./img/bangladesh.jpg')"},
{"key": "b", "title": "Sports", "image": "require('./img/sports.jpg')"},
{"key": "c", "title": "Politics", "image": "require('./img/politics.jpg')"},
{"key": "d", "title": "Entertainment", "image": "require('./img/entertainment.png')"},
{"key": "e", "title": "Economics", "image": "require('./img/economics.jpg')"},
{"key": "f", "title": "Technology", "image": "require('./img/technology.jpg')"},
{"key": "g", "title": "Others", "image": "require('./img/m.jpg')"},
]
to this 对此
[
{"key": "a", "title": "Bangladesh", "image": require('./img/bangladesh.jpg')},
{"key": "b", "title": "Sports", "image": require('./img/sports.jpg')},
{"key": "c", "title": "Politics", "image": require('./img/politics.jpg')},
{"key": "d", "title": "Entertainment", "image": require('./img/entertainment.png')},
{"key": "e", "title": "Economics", "image": require('./img/economics.jpg')},
{"key": "f", "title": "Technology", "image": require('./img/technology.jpg')},
{"key": "g", "title": "Others", "image": require('./img/m.jpg')},
]
Your image source is string. 您的图像源是字符串。 You should eval it.
您应该评估它。
<FlatList horizontal= {true}
data={newsCategory}
renderItem={({item}) => (
<TouchableOpacity
style={styles.option}
>
<Image
style={styles.imgButton}
source={eval(item.image)}
/>
<Text style={styles.buttonText}>{item.title}</Text>
</TouchableOpacity>
)
}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.