【发布时间】:2022-10-21 21:27:27
【问题描述】:
我在这里有我的代码,可以让我获得 btc 价格
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
我想在我的 UI 上每 2 分钟更新一次价格,如果我像现在一样使用它,当应用程序加载时,它在前 2 分钟显示 0,而不是价格更新,但它不会每 2 分钟更新一次
我怎样才能解决这个问题?
【问题讨论】:
标签: react-native fetch