When I make first api call, I get json data containing all the race cars info. Then when I make second api call to retrieve info for corresponding individual driver, I need driverId in api endpoint. So I tried mapping the driverId from the result of first api call, but then I get error. I'm trying to display all race cars with matching driver(ex. porshe with sally, since they both share same id). How can I achieve this result? https://codesandbox.io/s/polished-firefly-08siz?file=/src/App.js
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import './styles.css';
const App = () => {
const [raceCarData, setRaceCarData] = useState({ carData: null, driverData: null });
const raceCars = [
{'car': 'porsche', 'description': 'luxury', 'driverId': 4},
{'car': 'ferrari', 'description': 'beatiful', 'driverId': 2},
{'car': 'bmw', 'description': 'slow', 'driverId': 5}
]
/*
https://github.com/racedrivers/4 => {'name': 'sally', 'gender': 'female', 'id': 4}
https://github.com/racedrivers/2 => {'name': 'john', 'gender': 'male', 'id': 2},
https://github.com/racedrivers/5 => {'name': 'tyffany', 'gender': 'female', 'id': 5},
*/
useEffect(() => {
const getRaceCarData = async () => {
const cars = await axios.get("https://github.com/raceCars");
cars.data.map((res) => {
const drivers = await axios.get(
`https://github.com/racedrivers/${res.driverId}`
);
})
setRaceCarData({ carData: cars.data, driverData: drivers.data });
};
getRaceCarData();
}, []);
return (
<div>
<h1>{raceCardata.carData.map((value) => value.car}</h1>
<h2>{raceCardata.driverData.map((value) => value.name}</h2>
</div>
);
}
export default App;