I got the error when I tried to get some datas from api.
Then I searched how to solve this error but I couldn't solve. I want to solove this error. I don't know Why do I get this error. What should I fix my code ?? Please tell me any ideas. thank you for reading !
this is my error.
TypeError: Cannot read property 'map' of undefined
this is my code.
import React,{ Component } from 'react';
class Exchange extends Component {
constructor(props){
super(props);
this.state = {
isLoaded: false,
items: [],
}
}
componentDidMount(){
fetch('https://api.exchangeratesapi.io/latest')
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
items: json.items,
})
})
}
render(){
var { items,isLoaded } = this.state;
if(!isLoaded){
return <div>...Loading</div>;
}else{
return (
<div>
<ul>
{items.map(item =>(
<li key={item.rates}>{item.CAD}</li>
))}
</ul>
</div>
)
}
}
}
export default Exchange;