【发布时间】:2018-02-13 03:47:43
【问题描述】:
在这里反应新手。我有一些 React 代码,我试图循环遍历我在 .state 中提供的数组并显示图像。 (使用 'Image' 并使用来自 https://github.com/lhandel/react-native-card-stack-swiper 的 Card 对象来显示 Card 对象内的 Image,它本身嵌套在 Object 中,但这既不是这里也不是那里。)
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
cards: [
{itemname: 'Item 1',
restoname: 'Resto 1',
url: 'https://res.cloudinary.com/grubhub/image/upload/v1515191165/bkf4niv9okipbc8y6o8h.jpg',
description: 'Desc 1'},
{itemname: 'Item 2',
restoname: 'Resto 2',
url: 'https://res.cloudinary.com/grubhub/image/upload/v1514060352/twhriy3hvbzayktrjp91.jpg',
description: 'Desc 2'}
]
};
}
render() {
const contents = this.state.cards.map((item, index) => {
return (
<Card key={'Card_' + index}>
<Image
key={index}
source={{uri: item.url}} />
</Card>
)
});
return (
<View style={{flex:1}}>
<CardStack
cards = {this.state.cards}
>
<View>
{contents}
</View>
</CardStack>
我的不足之处是最后一部分 --- 如果我将内容定义为在其中返回 <Card> 和 <Image> 对象的映射,那么它不应该在最后出现我在<CardStack> 对象内调用{contents}?什么都没有显示,也没有错误消息。
另外,除了最初什么都没有显示之外,如果我滑动到下一张卡片,我会收到以下错误:
undefined 不是对象(评估 'this.state.cards[sindex - 2].props`)
【问题讨论】:
-
啊,想通了,我不需要
对象。可能不是很好的 JSX,但仅将 {contents}单独放置似乎就足够了。
标签: javascript reactjs react-native mapping