I have here my code that gets me the btc price
function Nft() {
const[price,setPrice]=useState(0);
setTimeout(() => {
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
.then((res)=>res.json())
.then((response)=>{
//your response is like this ,{"bitcoin":{"usd":18993.39}}
let price= response?.bitcoin?.usd;
setPrice(price)
})
.catch((error)=>{
//here you can manage errors
})
}, 12000);
return (
<View>
<Text style={style.header}>Bitcoin Price</Text>
<Text >{price}</Text>
</View>
);
};
export default Nft
function Nft() {
const[price,setPrice]=useState(0);
setTimeout(() => {
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
.then((res)=>res.json())
.then((response)=>{
//your response is like this ,{"bitcoin":{"usd":18993.39}}
let price= response?.bitcoin?.usd;
setPrice(price)
})
.catch((error)=>{
//here you can manage errors
})
}, 12000);
return (
<View>
<Text style={style.header}>Bitcoin Price</Text>
<Text >{price}</Text>
</View>
);
};
export default Nft
I want to update the price every 2 minutes on my UI, if I use it like it is right now, when the app loads it displays 0 for the first 2 mintes than the price gets updated, but it wont get updated every 2 minutes
HOw can i fix this?
CodePudding user response:
Replace the setTimeout with setInterval.
setTimeout
runs only once after the given timeout.