So i want to get live price update every 5 seconds therefore i put recursive setTimeout into the function. Unfortunately right now the price feed is working this way:
- At the start console outputs are: undefined and current price
- After price change outputs are: undefined, old price, new current price
- After next price change: undefined, oldest price, older price, new current price
why does it appending the values instead of overwriting them as usual?
import React, { useState, useEffect } from 'react';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import Livebsv from './livebsv.js';
const CoinGecko = require('coingecko-api');
const CoinGeckoClient = new CoinGecko();
export default function Calculatorbuy() {
const [value, setValue] = useState(0);
const [price, setPrice] = useState();
useEffect(() => {
const fetchPrice = async () => {
const result = await CoinGeckoClient.simple.price({
ids: "bitcoin-cash-sv",
vs_currencies: "pln",
});
setPrice(parseFloat(result.data.['bitcoin-cash-sv'].pln));
console.log(price);
setTimeout(fetchPrice, 5000)
};
fetchPrice();
})