javascript - React native:在路径数组上使用地图显示图像不起作用
问题描述
我是 react native 的初学者,我正在尝试显示一组路径的前三张照片。
import React from 'react';
import { View, Image, StyleSheet, FlatList, Dimensions, Text } from 'react-native';
import ToolBar from '../components/blocs/ToolBar'
import HeaderBlock from '../components/blocs/Header'
let pictures = require('../assets/pictures.json');
let { width: screenWidth } = Dimensions.get('window')
interface State { // Added this interface for props
My_photo: []
}
export class Photos extends React.Component<{}, State>{
constructor(props) {
super(props);
this.state = {
My_photo: []
}
}
GetFirstPicture(Pictures) {
let tab = []
for (let i = 0; i < Pictures.length; i++) {
if (i < 3) {
tab.push(Pictures[i].path)
}
else { break; }
}
return tab
}
UNSAFE_componentWillMount() {
let tab
tab = this.GetFirstPicture(pictures[0])
this.setState({
My_photo: tab
})
}
render() {
const { My_photo } = this.state
return (
<View>
<HeaderBlock />
<Text style={styles.title}>Mes photos</Text>
{console.log(My_photo)}
<View style={styles.photo_container}>
{My_photo.map((picturePath) => {
<Image source={{ uri: picturePath }} style={{
height: screenWidth / 3,
width: screenWidth / 3,
}} />
})}
</View>
<Text style={styles.title2}>Photos partagées avec moi</Text>
</View>
);
}
}
export default Photos;
这是我的图片.json 文件:
[
[
{
"id": 0,
"owner": "Jeremy",
"path": "https://pixnio.com/free-images/2017/03/07/2017-03-07-09-18-21.jpg"
},
{
"id": 1,
"owner": "Basile",
"path": "https://pixnio.com/free-images/2017/08/30/2017-08-30-07-06-43-1000x666.jpg"
},
{
"id": 2,
"owner": "Deb",
"path": "https://img.fotocommunity.com/paysage-dautomne-c3f5aa2f-5592-44ed-9d00-e3a6b96a6b58.jpg?height=1080"
},
{
"id": 3,
"owner": "Basile",
"path": "https://pixnio.com/free-images/2017/08/30/2017-08-30-07-06-43-1000x666.jpg"
},
{
"id": 4,
"owner": "Jeremy",
"path": "https://pixnio.com/free-images/2017/03/07/2017-03-07-09-18-21.jpg"
}
],
[
{
"id": 0,
"owner": "Jeremy",
"path": "https://pixnio.com/free-images/2017/03/07/2017-03-07-09-18-21.jpg"
},
{
"id": 1,
"owner": "Basile",
"path": "https://pixnio.com/free-images/2017/08/30/2017-08-30-07-06-43-1000x666.jpg"
},
{
"id": 2,
"owner": "Deb",
"path": "https://img.fotocommunity.com/paysage-dautomne-c3f5aa2f-5592-44ed-9d00-e3a6b96a6b58.jpg?height=1080"
}
]
]
问题是,My_photo 上的地图功能没有按应有的方式显示图像。
请问有什么想法吗?
谢谢
PS:如果您知道如何更换折旧的组件,我也会接受:D
解决方案
您的 map 函数有一个错误并且没有返回任何值,使用箭头符号您应该删除花括号或添加一个返回。
任何一个:
{My_photo.map((picturePath) => (
<Image
source={{ uri: picturePath }}
style={{
height: screenWidth / 3,
width: screenWidth / 3,
}}
/>
))}
或者:
{My_photo.map((picturePath) => {
return (
<Image
source={{ uri: picturePath }}
style={{
height: screenWidth / 3,
width: screenWidth / 3,
}}
/>
);
})}
推荐阅读
- java - 如何在 Java 9 的类路径中获取 jar 文件和文件夹的列表?
- gtk - 使用 GTK+ 3.0 将文本从一个应用程序插入另一个应用程序的惯用方式?
- primefaces - PrimeFaces 4.0 高级主题兼容性
- visual-studio-2017 - 在 VS 2017 中获取错误 CS1504 源文件'C:\temp\dashboard\
- reactjs - 如何在没有 CDN 的情况下部署 React 组件?
- r - 影响 RStudio 中函数源代码的注释行
- php - PHP SQL JOIN - 将辅助字段添加到结果
- video - 将 MPEG4 转换为 H264
- python - 时区感知日期
- java - 重新加载组合框值时,箭头键选择对自动完成的组合框不起作用 java Fx