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:
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();
})
useEffect
hook runs every time the component renders, so you are starting multiple timeouts. I suspect the "appending" you are seeing is the result of duplicate timeouts running the same callback.Code:
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));
};
fetchPrice(); // <-- first initial fetch
const timerId = setInterval(fetchPrice, 5000); // <-- start interval
return () => clearInterval(timerId); // <-- return cleanup
}, []); // <-- run on mount
useEffect(() => {
console.log(price); // <-- log updated state
}, [price]); // <-- run on price update