I'm trying to get Bitcoin Price from Coindesk API. This is my app:
import React from 'react';
import ReactDOM from 'react-dom';
let bpiURL = 'http://api.coindesk.com/v1/bpi/currentprice.json';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: {}
};
}
componentDidMount() {
fetch(bpiURL)
.then(response => response.json())
.then(res => {
console.log(res);
return res;
})
.then(response => this.setState({ data: response }));
}
render() {
return (
<div>
<p>
{this.state.data.disclaimer}
</p>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
The problem is that it works with this.state.data.disclaimer and this.state.data.chartName but it doesn't work with this.state.data.bpi.USD.rate which is what I need. How can I get that value?
EDIT: this is what I get from this.state.data :
Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. in p (at index.js:42) in div (at index.js:37) in App (at index.js:49)
console.log(this.state.data)instead of returning it inrenderfor debugging purposes.rateproperty under USD.