1👍
✅
chartData
should be an array.
Try the following approach.
//init state
this.state = {
chartData: [],
}
//service inside componentDidMount
axios.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=2&page=1&sparkline=true')
.then(res => {
const x = res.data;
let chartData = [];
x.forEach(element => {
chartData.push({
labels: element.sparkline_in_7d.price,
datasets:[{data: element.sparkline_in_7d.price}]
});
});
this.setState({chartData});
})
Here is the fiddle.
https://codesandbox.io/s/sharp-wildflower-23hy7
Hope it will helps you.
Source:stackexchange.com