我有两个问题。首先,当从 API 获取数据时,我只在我设置的时间间隔后才获取数据。但是,我需要立即接听第一个电话。其次,当我将接收到的数据转换为数字时,我丢失了部分数字,例如收到 11,518.6217 而我只得到 11。有人能帮帮我吗?
class Currencies extends Component {
state = {
inputField: 0,
exRates: 0,
};
componentDidMount() {
try {
setInterval(async () => {
const res = await fetch(
"https://api.coindesk.com/v1/bpi/currentprice.json"
);
const exRates = await res.json();
console.log(exRates.bpi);
this.setState({ exRates: exRates.bpi });
}, 6000);
} catch (err) {
console.log(err);
}
}
BTCChangeHandler = (event) => {
this.setState({ inputField: event.target.value });
};
render() {
const exRates = this.state.exRates;
const currData = Object.values(exRates).map((value) => value.rate);
const rates = currData.map(parseFloat);
console.log(rates);
return (
<div>
<input
type="number"
placeholder="Enter BTC value"
onChange={this.BTCChangeHandler}
/>
<Currency name="USD" value={this.state.inputField * rates[0]} />
<Currency name="GPB" value={this.state.inputField * rates[1]} />
<Currency name="EUR" value={this.state.inputField * rates[2]} />
</div>
);
}
}
export default Currencies;
ITMISS
小唯快跑啊
互换的青春
相关分类