【问题标题】:How can I update BTC price in real time from API- reactNative如何从 API-react Native 实时更新 BTC 价格
【发布时间】: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


    【解决方案1】:

    更换设置超时设置间隔.

    setTimeout 在给定超时后仅运行一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多