在 React Native 中渲染来自已解析对象数组的图像源
Render images sources from parsed array of objects in react native
我正在构建一个本机应用程序,它假设使用一些 'meta-data' 对象作为源。我正在解析数组中的每个对象并为每个 item
返回一个 JSX 布局。我唯一的问题是如何提供图像来源,因为我将它们存储在本地并且需要 require("link")
它们。我的代码给我错误:
function loadModuleImplementation(moduleId, module) {
moduleId = "../../../images/icons/photographer/Party.png"
我的代码:
class SelectScreen extends Component {
props:Props;
Details = [
{
"type": "Party",
"image": "../../../images/icons/photographer/Party.png"
},
{
"type": "Wedding",
"image": "../../../images/icons/photographer/Wedding.png"
},
{
"type": "Architecture",
"image": "../../../images/icons/photographer/Arhitecture.png"
},
{
"type": "Christening",
"image": "../../../images/icons/photographer/Christening.png"
}
];
render() {
let renderPhotoTypes = () => {
let type = [];
this.Details.map( ( item )=> {
type.push(
<View style={styles.imageSelectItem} key={item.type}>
<TouchableWithoutFeedback>
<View>
<View style={styles.imageContainer}>
<Image style={styles.image} source={require(item.image)}/>
</View>
<Text style={styles.text}>{item.type}</Text>
</View>
</TouchableWithoutFeedback>
</View>
);
} );
return type;
};
return (
<ScrollView style={styleGuide.containerMain}>
<Text style={styleGuide.heading_1}>
Select type
</Text>
<View style={styles.imageSelectContainer}>
{renderPhotoTypes()}
</View>
</ScrollView>);
}
}
版本:
"dependencies": {
"axios": "^0.12.0",
"babel-relay-plugin": "^0.9.0",
"buffer": "^4.6.0",
"color": "^0.11.1",
"invariant": "^2.2.1",
"react": "~15.0.2",
"react-native": "^0.26.1",
"react-native-button": "^1.6.0",
"react-native-drawer": "^2.2.3",
"react-native-fbsdk": "^0.2.1",
"react-native-message-bar": "^1.6.0",
"react-native-radio-buttons": "^0.11.0",
"react-native-vector-icons": "^2.0.3",
"react-redux": "^4.4.5",
"react-relay": "^0.9.0",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-persist": "^3.2.2",
"redux-thunk": "^2.1.0"
}
你可以这样做
Details = [
{
type: "Party",
image: require("../../../images/icons/photographer/Party.png")
},
{
type: "Wedding",
image: require("../../../images/icons/photographer/Wedding.png")
},
{
type: "Architecture",
image: require("../../../images/icons/photographer/Arhitecture.png")
},
{
type: "Christening",
image: require("../../../images/icons/photographer/Christening.png")
}
];
render() {
let renderPhotoTypes = () => {
let type = [];
this.Details.map( ( item )=> {
type.push(
<View style={styles.imageSelectItem} key={item.type}>
<TouchableWithoutFeedback>
<View>
<View style={styles.imageContainer}>
<Image style={styles.image} source={item.image}/>
</View>
<Text style={styles.text}>{item.type}</Text>
</View>
</TouchableWithoutFeedback>
</View>
);
} );
return type;
};
我正在构建一个本机应用程序,它假设使用一些 'meta-data' 对象作为源。我正在解析数组中的每个对象并为每个 item
返回一个 JSX 布局。我唯一的问题是如何提供图像来源,因为我将它们存储在本地并且需要 require("link")
它们。我的代码给我错误:
function loadModuleImplementation(moduleId, module) {
moduleId = "../../../images/icons/photographer/Party.png"
我的代码:
class SelectScreen extends Component {
props:Props;
Details = [
{
"type": "Party",
"image": "../../../images/icons/photographer/Party.png"
},
{
"type": "Wedding",
"image": "../../../images/icons/photographer/Wedding.png"
},
{
"type": "Architecture",
"image": "../../../images/icons/photographer/Arhitecture.png"
},
{
"type": "Christening",
"image": "../../../images/icons/photographer/Christening.png"
}
];
render() {
let renderPhotoTypes = () => {
let type = [];
this.Details.map( ( item )=> {
type.push(
<View style={styles.imageSelectItem} key={item.type}>
<TouchableWithoutFeedback>
<View>
<View style={styles.imageContainer}>
<Image style={styles.image} source={require(item.image)}/>
</View>
<Text style={styles.text}>{item.type}</Text>
</View>
</TouchableWithoutFeedback>
</View>
);
} );
return type;
};
return (
<ScrollView style={styleGuide.containerMain}>
<Text style={styleGuide.heading_1}>
Select type
</Text>
<View style={styles.imageSelectContainer}>
{renderPhotoTypes()}
</View>
</ScrollView>);
}
}
版本:
"dependencies": {
"axios": "^0.12.0",
"babel-relay-plugin": "^0.9.0",
"buffer": "^4.6.0",
"color": "^0.11.1",
"invariant": "^2.2.1",
"react": "~15.0.2",
"react-native": "^0.26.1",
"react-native-button": "^1.6.0",
"react-native-drawer": "^2.2.3",
"react-native-fbsdk": "^0.2.1",
"react-native-message-bar": "^1.6.0",
"react-native-radio-buttons": "^0.11.0",
"react-native-vector-icons": "^2.0.3",
"react-redux": "^4.4.5",
"react-relay": "^0.9.0",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-persist": "^3.2.2",
"redux-thunk": "^2.1.0"
}
你可以这样做
Details = [
{
type: "Party",
image: require("../../../images/icons/photographer/Party.png")
},
{
type: "Wedding",
image: require("../../../images/icons/photographer/Wedding.png")
},
{
type: "Architecture",
image: require("../../../images/icons/photographer/Arhitecture.png")
},
{
type: "Christening",
image: require("../../../images/icons/photographer/Christening.png")
}
];
render() {
let renderPhotoTypes = () => {
let type = [];
this.Details.map( ( item )=> {
type.push(
<View style={styles.imageSelectItem} key={item.type}>
<TouchableWithoutFeedback>
<View>
<View style={styles.imageContainer}>
<Image style={styles.image} source={item.image}/>
</View>
<Text style={styles.text}>{item.type}</Text>
</View>
</TouchableWithoutFeedback>
</View>
);
} );
return type;
};