Я извлекаю список постов из моего API, и я бы хотел расположить их, например, ->
<Row>
<Col><Card.......></Col>
<Col><Card.......></Col>
<Col><Card.......></Col>
<Col><Card.......></Col>
<Row>
Я посмотрел вокруг и нашел несколько примеров, но я тоже "«ReactJS» или они просто не работают в моем случае.
Так выглядит мой класс на данный момент ->
class ApiPosts extends Component {
constructor() {
super();
this.state = {
blogPosts: [],
};
}
componentDidMount() {
const { classes } = this.props;
fetch('http://localhost:53595/api/public/posts', {
method: 'get',
})
.then(results => {
return results.json();
}).then(data => {
let blogPosts = data.map((post, index) => {
if (post.type === "News") {
if ((index % 4) === 0) {
return (
<Col xs >
<Card className={classes.card} key={post.id}>
<CardActionArea>
<CardMedia
component="img"
alt={post.title}
className={classes.media}
height="140"
image={post.imageName}
title={post.title}
/>
<CardContent>
<Typography gutterBottom variant="headline" component="h2">
{post.title}
</Typography>
<Typography component="p">
{post.body}
</Typography>
</CardContent>
</CardActionArea>
<CardActions>
<Button size="small" color="primary">
Share
</Button>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Card>
</Col>
)
}
}
});
this.setState({ blogPosts: blogPosts });
//console.log("state", this.state.blogPosts);
})
}
render() {
return (
<React.Fragment>
<Col xs />
{this.state.blogPosts}
</React.Fragment>
)
}
}
Есть ли способ добиться этого с помощью карты?Извините за мой вопрос новичка, все еще изучающий ReactJS.Большое спасибо заранее!