I am trying to render the response of a simple api I've created but when I try to iterate through the items, it comes as undefined. I know it has to do with asynchronous programming but can't figure out how to solve it. Could anybody help ?
import React from 'react';
export default class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
items: undefined
};
}
componentDidMount() {
fetch('https://petzie-cajuhqnpqy.now.sh/breeds')
.then(res => res.json())
.then(data => {
this.setState(() => {
return {
items: data.result
};
});
console.log(this.state);
})
.catch(err => console.log(err));
}
render() {
return (
<div>
{ this.state.items.map((item) => {
return <p>{item.name}</p>;
}) }
</div>
);
}
}