interface Ticker { price: number; percent: number; usdVolume: number; } const ticker =…
- User
interface Ticker { price: number; percent: number; usdVolume: number; }
const ticker = useSelector((state: AppState) => state.binanceTicker.futures); //futures: {[key: string]: Ticker}; // 1INCHUSDT: {price: 0.306, percent: -1.671, usdVolume: 25744127.004} //1000FLOKIUSDT: {price: 0.0218, percent: -0.502, usdVolume: 4052385.7154} ....
const [coins, setCoins] = useState<Ticker[]>([]);
useEffect(() => { const interval = setInterval(() => { // напиши код для выбора 20 монет с наибольшим usdVolume и его нужно сетать в usestate coins }, 60000); // 60 000 миллисекунд = 60 секунд
return () => clearInterval(interval); // Очистка интервала при размонтировании компонента}, [])
потом этот сте
- Assistant
In order to select the 20 coins with the highest usdVolume, you can use the following code inside the setInterval callback:
const selectedCoins = Object.values(ticker).sort((a, b) => b.usdVolume - a.usdVolume).slice(0, 20); setCoins(selectedCoins);Explanation:
- Object.values(ticker) retrieves an array of all the ticker objects.
- The sort() method is used to sort the array of ticker objects based on the usdVolume property in descending order.
- The slice() method is used to select only the first 20 ticker objects.
- The selectedCoins array is then set as the state using the setCoins function.
This code will run every 60 seconds, as specified by the interval. The selected coins will be updated in the coins state.
- User
теперь coins нужно прогонять через map и добавлять в компонент
- Assistant
To map and render the
coinsarray in your component, you can use themapmethod to iterate through eachTickerobject and create aBinanceLiveCoincomponent for each coin. Here's an example:return ( <div> {coins.map((coin, index) => ( <BinanceLiveCoin key={index} symbol={coin.symbol} name={coin.name} /> ))} </div> );Explanation:
- The
mapmethod is used to iterate through each element of thecoinsarray. - For each
Tickerobject in thecoinsarray, aBinanceLiveCoincomponent is rendered. - The
keyprop is set to theindexto help React efficiently update and track the components. - The
symbolprop is set to thesymbolproperty of theTickerobject. - The
nameprop is set to thenameproperty of theTickerobject.
Make sure to replace
'btcusdt'and'BTC'with the appropriate values from eachTickerobject in thecoinsarray.Note: It seems like the
symbolandnameproperties are not present in theTickerinterface you provided. You may need to modify the interface or provide the appropriate properties in theTickerobjects in order to use them in theBinanceLiveCoincomponent. - The