Pretty new to React and I have this issue: I want to get URL+ImageUrl from coinData.
In Vue, I would just create a method in the component:
getCoinImage: function(symbol) {
return CRYPTOCOMPARE_API_URI + this.coinData[symbol].ImageUrl;
}
Then call the function in the view template and it would return full URL of the image I'm looking for.
How do I do it in React?
class App extends Component {
constructor(props) {
super(props);
this.state = {
cryptos: [],
coinData: {},
};
}
componentDidMount() {
axios.get('https://api.coinmarketcap.com/v1/ticker/?limit=0')
.then(res => {
const cryptos = res.data;
this.setState({cryptos: cryptos});
console.log(this.state.cryptos);
})
axios.get('https://min-api.cryptocompare.com/data/all/coinlist')
.then(res => {
const coinData = res.data.Data;
this.setState({coinData: coinData});
//console.log(coinData["BTC"].ImageUrl);
})
}
render() {
return (
<table className="table is-striped is-narrow is-fullwidth"><thead>
<tr>
<td>Rank</td>
<td>Name</td>
<td>Symbol</td>
<td>Price (USD)</td>
<td>1h</td>
<td>24h</td>
<td>1 Week</td>
<td>Market Cap</td>
</tr>
</thead>
<tbody>{Object.keys(this.state.cryptos).map((key) => (
<tr>
<td>{this.state.cryptos[key].rank}</td>
<td>{this.state.cryptos[key].name}</td>
<td>{this.state.cryptos[key].symbol}</td>
<td>{this.state.cryptos[key].price_usd}</td>
<td>{this.state.cryptos[key].percent_change_1h}</td>
<td>{this.state.cryptos[key].percent_change_24h}</td>
<td>{this.state.cryptos[key].percent_change_7d}</td>
<td>{this.state.cryptos[key].market_cap_usd}</td>
</tr>
))};
</tbody>
</table>
);
}
}
this.state.cryptosis an array. Why not usethis.state.cryptos.mapdirectly?