I am trying fetch data from API in my React JS component. Link to API. This API will populate the number count of each vehicle. The last argument 'audi' corresponds to vehicle make name. I need to fetch that data for 64 different vehicles and create dynamic list items (li) but don't know how to do that. Everything is working except fetchCount function.
Here is an example of vehicles data which is imported from '../shared/vehicle_make_and_models' there are total 64 care makes.
const veh_data = [
{ "alfa-romeo": ["145", "90", "Alfa 6", "Alfasud"] },
{ "aston-martin": ["15", "2-Litre", "AM Vantage", "Atom", "Cygnet", "DB2"]},
{ "audi": ["100", "200", "A1", "A2", "A3", "A4", "A5", "A6", "A7"] }
];
Here is my code:
import React, { Component } from 'react';
import { veh_data } from '../shared/vehicle_make_and_models'
class ShopByMake extends Component {
constructor(props) {
super(props);
this.fetchCount = this.fetchCount.bind(this);
this.state = {
veh_data: veh_data,
};
}
fetchCount(make) {
fetch('https://mysterious-journey-51969.herokuapp.com/api/vehicle-make-count/' + make)
.then(response => {
return response.json();
})
.then(data => {
let firstKey = Object.keys(data)[0],
count = data[firstKey];
console.log('make count' + count);
return count;
})
.catch(err => console.log(err));
}
render() {
const vehicles = this.state.veh_data.reduce((acc, veh) => {
let make = Object.keys(veh)[0]
return {
makes: [
...acc.makes,
<li className="mt-2"><a href="#"><img src={require('../assets/images/audi-logo.jpg')} className="img-fluid logos" /><span className="ml-4 list-text">{make} ({this.fetchCount(make)})</span></a></li>
]
};
}, { makes: [] });
return (
<div>
<div className="headings-div text-center text-white mt-4 mt-lg-0"><h5 className="headings">Shop By Make</h5></div>
<div className="mt-3" id="shopbymake">
<ul className="list-unstyled">
{vehicles.makes}
</ul>
</div>
</div>
);
}
}
export default ShopByMake;