我怎样才能用数组制作卡片?
How can i make cards out of an array?
我有一个由许多项目组成的数组,用于以类似于列表的方式制作卡片,但呈现为卡片并遵循卡片的结构。
这是我的数组:
const INFO = [{
id: 1,
icon: require("../../../assets/logo.png"),
title: 'Negocio',
subtitle: 'Rubro',
body: 'Descuento',
accordeon: dataArray },
{
id: 2,
icon: require("../../../assets/logo.png"),
title: 'Segundo Negocio',
subtitle: 'Segundo Rubro',
body: 'Descuento 2',
accordeon: dataArray
}];
我想以与这张卡片类似的方式显示它:
<Container style={styles.container}>
<Header>
<Body style={{ flex: 5 }}>
<Title>Titulo</Title>
</Body>
<Right/>
</Header>
<Content padder key={item.id}>
<Card key={index} style={styles.mb}>
<CardItem bordered>
<Left>
<Thumbnail source={item.icon}/>
<Body>
<Text>item.title</Text>
<Text note>item.subtitle</Text>
</Body>
</Left>
</CardItem>
<CardItem>
<Body>
<Text>
body
</Text>
</Body>
</CardItem>
<CardItem style={{ paddingVertical: 0 }}>
<Left>
<Content padder style={{ backgroundColor: "white" }}>
<Accordion dataArray={accordeon} animation={false}/>
</Content>
</Left>
</CardItem>
</Card>
</Content>
</Container>
那么我怎样才能让它发挥作用呢?
您可以将卡片数据数组传递到 flatlist 并为每个项目呈现一张卡片,如下所示:
export default class CardList extends Component {
constructor(props) {
super(props);
this.state = {
cards: [
{
id: 1,
icon: require("../../../assets/logo.png"),
title: 'Negocio',
subtitle: 'Rubro',
body: 'Descuento',
accordeon: dataArray
},
{
id: 2,
icon: require("../../../assets/logo.png"),
title: 'Segundo Negocio',
subtitle: 'Segundo Rubro',
body: 'Descuento 2',
accordeon: dataArray
}
]
}
}
render() {
return (
<FlatList
data={this.state.cards}
renderItem={this.renderCard}
/>
)
}
renderCard = ({item}) => (
<Container style={styles.container}>
<Header>
<Body style={{flex: 5}}>
<Title>Titulo</Title>
</Body>
<Right/>
</Header>
<Content padder key={item.id}>
<Card key={index} style={styles.mb}>
<CardItem bordered>
<Left>
<Thumbnail source={item.icon}/>
<Body>
<Text>item.title</Text>
<Text note>item.subtitle</Text>
</Body>
</Left>
</CardItem>
<CardItem>
<Body>
<Text>
body
</Text>
</Body>
</CardItem>
<CardItem style={{paddingVertical: 0}}>
<Left>
<Content padder style={{backgroundColor: "white"}}>
<Accordion dataArray={accordeon} animation={false}/>
</Content>
</Left>
</CardItem>
</Card>
</Content>
</Container>
)
}
我有一个由许多项目组成的数组,用于以类似于列表的方式制作卡片,但呈现为卡片并遵循卡片的结构。 这是我的数组:
const INFO = [{
id: 1,
icon: require("../../../assets/logo.png"),
title: 'Negocio',
subtitle: 'Rubro',
body: 'Descuento',
accordeon: dataArray },
{
id: 2,
icon: require("../../../assets/logo.png"),
title: 'Segundo Negocio',
subtitle: 'Segundo Rubro',
body: 'Descuento 2',
accordeon: dataArray
}];
我想以与这张卡片类似的方式显示它:
<Container style={styles.container}>
<Header>
<Body style={{ flex: 5 }}>
<Title>Titulo</Title>
</Body>
<Right/>
</Header>
<Content padder key={item.id}>
<Card key={index} style={styles.mb}>
<CardItem bordered>
<Left>
<Thumbnail source={item.icon}/>
<Body>
<Text>item.title</Text>
<Text note>item.subtitle</Text>
</Body>
</Left>
</CardItem>
<CardItem>
<Body>
<Text>
body
</Text>
</Body>
</CardItem>
<CardItem style={{ paddingVertical: 0 }}>
<Left>
<Content padder style={{ backgroundColor: "white" }}>
<Accordion dataArray={accordeon} animation={false}/>
</Content>
</Left>
</CardItem>
</Card>
</Content>
</Container>
那么我怎样才能让它发挥作用呢?
您可以将卡片数据数组传递到 flatlist 并为每个项目呈现一张卡片,如下所示:
export default class CardList extends Component {
constructor(props) {
super(props);
this.state = {
cards: [
{
id: 1,
icon: require("../../../assets/logo.png"),
title: 'Negocio',
subtitle: 'Rubro',
body: 'Descuento',
accordeon: dataArray
},
{
id: 2,
icon: require("../../../assets/logo.png"),
title: 'Segundo Negocio',
subtitle: 'Segundo Rubro',
body: 'Descuento 2',
accordeon: dataArray
}
]
}
}
render() {
return (
<FlatList
data={this.state.cards}
renderItem={this.renderCard}
/>
)
}
renderCard = ({item}) => (
<Container style={styles.container}>
<Header>
<Body style={{flex: 5}}>
<Title>Titulo</Title>
</Body>
<Right/>
</Header>
<Content padder key={item.id}>
<Card key={index} style={styles.mb}>
<CardItem bordered>
<Left>
<Thumbnail source={item.icon}/>
<Body>
<Text>item.title</Text>
<Text note>item.subtitle</Text>
</Body>
</Left>
</CardItem>
<CardItem>
<Body>
<Text>
body
</Text>
</Body>
</CardItem>
<CardItem style={{paddingVertical: 0}}>
<Left>
<Content padder style={{backgroundColor: "white"}}>
<Accordion dataArray={accordeon} animation={false}/>
</Content>
</Left>
</CardItem>
</Card>
</Content>
</Container>
)
}