I am working on a simple crypto ticker app . I am using an api to get details about cryptos and this is how fetching look likes currently.
const [ Data , setData ] = useState([]);
useEffect(() => {
const interval = setInterval(() => {
fetch('https://api.coindcx.com/exchange/ticker').then((result) => {
result.json().then((resp) => {
console.log(resp)
setData(resp);
console.log(Data)
setLoading(false);
})
})
}, 3000);
return () => clearInterval(interval);
},[])
I am using interval so that data is updated every 3 seconds.
The problem I am facing is that in the useEffect setData is not setting data. console.log(resp)
is working correctly but console.log(Data)
is loggin out an empty array in console whereas I want data and resp to be the same.