Here is the component I am building. I have a dummy backend API that contains a list of fruits, and the response data looks as such:
{
"data": {
"1": {
"apple": "Delicious"
},
"2": {
"orange": "It's orange."
}
},
"message": "success retrieved fruits list",
"status": 200
}
Now I am building a component that calls my API (successfully) and I am trying to create a list of the items within data.
class FruitList extends Component {
constructor(props) {
super(props);
this.state = {
fruits: {},
error: null
}
}
componentDidMount() {
fruitsApi.getFruits()
.then(response => response.json())
.then(
(results) => {
this.setState({
fruits: results.data,
error: false
});
},
(error) => {
this.setState({ error: true });
console.log(error);
}
)
}
getFruits() {
const items = this.state.fruits.map(
([k, v]) => <li key={k}>{v}</li>
);
}
render() {
return (
<Container>
<Row>
<ul>
{ this.getFruits() }
</ul>
</Row>
</Container>
);
}
}
export default FruitList
From this, I result in a TypeError: this.state.fruits.map is not a function
. I have tried looking at other examples/errors similar, but can't seem to reason why my case is not acting properly. Any thoughts?
Thanks!