gpt4 book ai didi

reactjs - 如何在 NativeBase 中渲染数组中的卡片项?

转载 作者:行者123 更新时间:2023-12-03 14:20:11 26 4
gpt4 key购买 nike

我的代码无法运行。我想从对象数组中渲染卡片。然而,使用 NativeBase 似乎很难。

CardsScreen.js:

import React, { Component } from 'react';
import { Image } from 'react-native';
import { Container, Header, Content, Card, CardItem, Thumbnail, Text, Button, Icon, Left, Body, Right } from 'native-base';

class CardsScreen extends Component {
props:Props;


DATA = [
{ id: 1, title: 'Lorem ipsum dolor sit amet, everti rationibus his cu', views:'200', comments:'9', published:'4h ago' image: require('../img/img1.jpeg') },

{ id: 2, title: 'Lorem ipsum dolor sit amet, everti rationibus his ', Views:'700', comments:'16', published:'9h ago' image: require ('../img/img2.jpg') },

{ id: 3, title: 'Lorem ipsum dolor sit amet, everti rationibus hi', Views:'698', comments:'8', published:'14h ago' image:require ('../img/img3.jpeg') },

];

render() {

let renderpostTypes = () => {

let post = [];


this.DATA.map( ( item )=> {

post.push(

<Content key={item.id}>

<Card>
<CardItem>
<Left>
<Thumbnail source={require(item.image)} />
<Body>
<Text>item.title</Text>
<Text note>GeekyAnts</Text>
</Body>
</Left>
</CardItem>
<CardItem cardBody>
<Image source={require(item.image)} style={{height: 200, width: null, flex: 1}}/>
</CardItem>
<CardItem>
<Left>
<Button transparent>
<Icon active name="thumbs-up" />
<Text>item.views</Text>
</Button>
</Left>
<Body>
<Button transparent>
<Icon active name="chatbubbles" />
<Text>item.comments</Text>
</Button>
</Body>
<Right>
<Text>item.published</Text>
</Right>
</CardItem>
</Card>
</Content>
);
} );

return post;
};

return (
<Container>

<Content >
{renderpostTypes()}
</Content>
</Container>);
}
}
export default CardsScreen;

我怎样才能让它工作并从该对象数组中渲染卡片?有没有解决这个问题的例子?

最佳答案

我已经尝试过你的代码,它在我的设备上有效。但如果您想在 native 基础中使用数组来渲染卡,我有一个示例代码,这是代码:

import React, { Component } from 'react';
import { AppRegistry, Image } from 'react-native';
import { Container, Header, Content, Card, CardItem, Thumbnail, Text, Button, Icon, Left, Body, Right } from 'native-base';

class CardsScreen extends Component {
constructor(props) {
super(props);
this.state = {
DATA : [
{ id: 1, title: 'Lorem ipsum dolor sit amet, everti rationibus his cu', views:'200', comments:'9', published:'4h ago', image: '../img/img1.jpeg' },

{ id: 2, title: 'Lorem ipsum dolor sit amet, everti rationibus his ', Views:'700', comments:'16', published:'9h ago', image: '../img/img1.jpeg' },

{ id: 3, title: 'Lorem ipsum dolor sit amet, everti rationibus hi', Views:'698', comments:'8', published:'14h ago', image: '../img/img1.jpeg' },
],
}
}
render() {
return (
<Container>
<Content>
{this.state.DATA.map((item, index) => {
return(
<Card key={index} >
<CardItem>
<Left>
<Thumbnail source={require(item.image)}/>
<Body>
<Text>{item.title}</Text>
<Text note>GeekyAnts</Text>
</Body>
</Left>
</CardItem>
<CardItem cardBody>
<Image source={require(item.image)} style={{height: 200, width: null, flex: 1}} />
</CardItem>
<CardItem>
<Left>
<Button transparent>
<Icon active name="thumbs-up" />
<Text>{item.views}</Text>
</Button>
</Left>
<Body>
<Button transparent>
<Icon active name="chatbubbles" />
<Text>{item.comments}</Text>
</Button>
</Body>
<Right>
<Text>{item.published}</Text>
</Right>
</CardItem>
</Card>
);
})}
</Content>
</Container>);
}
}

AppRegistry.registerComponent('ExampleApps', () => CardsScreen);

希望能帮到你:)

关于reactjs - 如何在 NativeBase 中渲染数组中的卡片项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45409554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com