我正在阅读一个 JSON 文件,其中包含某些人的姓名和图像 URI。在迭代结构时,我可以打印名称但无法显示图像。我还读到 React 不支持动态图像,所以我按照此处的建议做了一个解决方法。
JSON
[
{
"id": 1,
"name": "Rohit",
"uri": "assets:/rohit.jpg",
"special": "text1"
},
{
"id": 2,
"name": "Anmol",
"uri": "assets:/rohit.jpg",
"special": "text2"
},
{
"id": 3,
"name": "Bhavya",
"uri": "assets:/rohit.jpg",
"special": "text3"
}
];
主要组件
import React, {Component} from 'react';
import { StyleSheet, Text, View, Image, ScrollView, Button, TouchableWithoutFeedback, TextInput, AsyncStorage} from 'react-native';
import { createStackNavigator } from 'react-navigation';
import customData1 from './customData.json';
class HomeScreen extends React.Component {
render() {
const initialArr = customData1;
return (
<View style={styles.container}>
<ScrollView vertical={true} contentContainerStyle={{flexGrow: 1}}>
<Text style={styles.category}>Category 1</Text>
<ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
<TouchableWithoutFeedback onPress={() => {
this.props.navigation.navigate('Details', {});
}}>
<View style={styles.view}>
{initialArr.map((prop, key) => {
return (
<View style={styles.container}>
<Image source={{uri: {prop.uri}}} style={styles.image}></Image>
<Text key={key}>{prop.uri}</Text>
</View>
);
})}
</View>
</TouchableWithoutFeedback>
</ScrollView>
</ScrollView>
</View>
)
}
}