如何通过JSON在React Native中要求本地图像?

时间:2019-11-12 08:18:19

标签: json image react-native

我正在尝试在React Native FlatList中加载本地图像,并且所有路径都保存在JSON中。

所有图像都存储在“ MY RN PROJECT” / src / assets / images /中。我尝试了以下方法:

<Image source={require(item.image)} style={{width: 100, height: 100}} />
//Not working, with error message "Transform Error... Invalid call require"
<Image source={{uri: "../../assets/images/gifts.jpg"}} style={{width: 100, height: 100}} />
//Not working, No Error Message
<Image source={require('../../assets/images/gifts.jpg')} style={{width: 100, height: 100}} />
//Works fine but this is not what I am looking for!

这是JS:

<FlatList
data={this.state.data}
extraData={this.state.data}
keyExtractor={(item) => item.name.toString()}
ListFooterComponent={this.renderFooter}
renderItem={({ item }) => (
<View>
<Image source={require(item.image)} style={{width: 100, height: 100}} />
</View>
)}
/>

这是JSON:

{
    "fr": [{
        "name": "Gifts",
        "image": "../../assets/images/gifts.jpg"
     }]
}

我正在使用: “ react-native”:“ 0.59.5”

任何帮助将不胜感激。谢谢!

0 个答案:

没有答案