I'm using React for an application and I need to get some values from a JSON API.
API sample.
{
"Meta Data": {
"1. Information": "Intraday Prices and Volumes for Digital Currency",
"2. Digital Currency Code": "BTC",
"3. Digital Currency Name": "Bitcoin",
"4. Market Code": "EUR",
"5. Market Name": "Euro",
"6. Interval": "5min",
"7. Last Refreshed": "2018-08-15 22:00:00",
"8. Time Zone": "UTC"
},
"Time Series (Digital Currency Intraday)": {
"2018-08-15 22:00:00": {
"1a. price (EUR)": "5648.06994794",
"1b. price (USD)": "6409.19375195",
"2. volume": "29868.07078908",
"3. market cap (USD)": "191430252.68421999"
},
Component:
import React, { Component } from 'react';
import Header from './Layout/Header/header';
import Tabela from './tabela';
class Page extends Component {
constructor() {
super();
this.state = {
ultimoValor: [],
valor: []
};
this.getTime();
}
getTime() {
let url = "https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_INTRADAY&symbol=BTC&market=EUR&apikey=GU3DEGQIB86G05S2";
fetch(url).then((response) => response.json()).then((responseJson) =>
{
this.setState({
valor: responseJson
});
})
}
render() {
return (
<div>
<Header />
<Tabela valor={this.state.valor}/>
</div>
);
}
}
But in the API, the given date ["Time Series (Digital Currency Intraday)"][ "2018-08-15 22:00:00"]
is updated constantly and I tried the following in the getTime () function:
getTime() {
let url = "https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_INTRADAY&symbol=BTC&market=EUR&apikey=GU3DEGQIB86G05S2";
fetch(url).then((response) => response.json()).then((responseJson) =>
{
this.setState({
ultimoValor: responseJson["Meta Data"]["7. Last Refreshed"],
valor: responseJson["Time Series (Digital Currency Intraday)"][{this.state.ultimoValor}]["1a. price (EUR)"]
});
})
}
I believe the error is in the assignment of valor
, because when I pass [{this.state.ultimoValor}]
it does not take the state value.