Chce wyświetlić dane z API w reaccie które zwraca takiego JSON'a:
{
"data": [
{
"slug": "song1",
"_id": "5dfa9511a71ef835bc3216ba",
"title": "song1",
"createdAt": "2019-12-18T21:07:29.093Z",
"updatedAt": "2019-12-26T17:44:07.852Z",
"__v": 0
},
{
"slug": "song2",
"_id": "5dfa4609c12e5b29546a59a6",
"title": "song2",
"createdAt": "2019-12-18T15:30:17.767Z",
"updatedAt": "2019-12-26T17:43:42.854Z",
"__v": 0
}
],
"count": 2
}
W reakcie wygląda to tak:
import React, { Component } from 'react';
export default class Customers extends Component {
constructor() {
super();
this.state = {
customers: []
};
}
componentDidMount() {
fetch('http://localhost:3000')
.then(res => res.json())
.then(customers =>
this.setState({ customers }, () => console.log('Fetch data', customers))
);
}
render() {
return (
<div>
<h2>Data: </h2>
<ul>{this.state.customers[1]}</ul>
</div>
);
}
}
W konsoli fetch zwraca mi takie coś:
I tutaj mam problem, w jaki sposób mogę teraz te dane wyrenderować?
Próbowałem w taki sposób:
{this.state.customers.map(res => (
<div>{res.title}</div>
))}
Zwraca błąd
TypeError: this.state.customers.map is not a function
Ktoś może nakierować jak rozwiązać mój problem?
Dziękuje, pozdrawiam