I'm currently trying to take data from an API so I can use it on my page. I'm programming with React and I am trying to use useEffect
with an async function.
Can someone please tell me why it breaks on a page refresh?
import React, { useEffect, useState } from 'react'
import axios from 'axios'
import './Cryptoinfo.css'
function Cryptoinfo() {
const [coinprice, setCoinprice] = useState([])
const url = `https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur`
useEffect(() => {
async function fetchData() {
try {
const result = await axios.get(url)
setCoinprice(result.data.slice(0, 10))
console.log(result)
} catch (e) {
console.error(e)
}
}
fetchData()
}, [url])
return (
<>
<div className='crypto-info-container'>
<div className='name-pic'>
<h3 className='crypto-info-img'>O</h3>
<h3 className='crypto-info-name'>Name</h3> //this should be coinprice[0].name but it breaks if i do that.
</div>
<h3 className='crypto-info-price'>Price</h3>
<h3 className='crypto-info-mc'>Marketcap</h3>
<h3 className='crypto-info-vol'>Volume (24hr)</h3>
<h3 className='crypto-info-sup'>Circulating supply</h3>
</div>
</>
)
}
export default Cryptoinfo
coinprice[0].id
}`. It simply means when Data is set in state...Render it – kazmi066 Nov 13 '21 at 13:33