I am trying to create a table with different crypto currencies and their values using react and coingecko api. I am new to api and api calls and every time i make a call i keep getting TypeError: Cannot read properties of undefined (reading 'map')
.
const [crypData, setCrypData] = useState([]);
const apiGet = () =>{
fetch('https://api.coingecko.com/api/v3/exchange_rates')
.then(response => {
if(response.ok){
return response.json()
}
throw response;
})
.then(res => {
console.log(res);
setCrypData(res.data);
console.log("crypData");
});
};
useEffect(() => {
apiGet();
},[])
return (
<>
{crypData.map(item =>
<tr >
<td scope="row">{item.rates.btc.name} </td>
<td>{item.rates.btc.unit} </td>
<td>{item.rates.btc.value} </td>
<td>
{item.rates.btc.type}
<button type="button"></button>
</td>
</tr>
)}
</>
);
}
export default DataTables;
When i change the response i am still getting this TypeError: crypData.map is not a function
.then(res => {
console.log(res);
setCrypData(res);
console.log("crypData");
});