I'm trying to extract deep data from a Json, but when I navigate to my screen, my code executes the first promise, but the second is a promise that maps through my hook that I've set up in the previous promise. I'm trying to figure out how to execute the second promise only when the first one is resolved. Here is my code:
const [tableHead, setTableHead] = useState('');
const [performance, setPerformance] = useState('');
const [performanceKpis, setPerformanceKpis] = useState('');
useEffect(() => {
async function getKpis() {
const findData = await Promise.all(
json.map((performance) => performance.performance)
);
setPerformance(...findData);
const findValues = await Promise.all( <=============== This is the stuff
setPerformanceKpis(performance.map((performance) => performance.value))
);
// setPerformanceKpis(findValues);
};
getKpis()
}, []);
I'm pretty new to promises. I didn't figured out how it works correctly.
[
{
"id": "3c20fbe3-fc59-433d-b349-6aa3b4a4c17b",
"name": "20210504",
"external_id": "3ad0c67e-170c-4ea8-afcc-850c94f449aa",
"farm_id": "489cd6a6-db19-4a6f-bc95-1d981edc972f",
"farm_name": "Granja Presence",
"producer_id": null,
"producer_name": "Produtor Granja Presence",
"type": "limited",
"finished": true,
"walkthrough_available": false,
"performance": [
{
"key": "AVERAGE_DATE_INPUT",
"name": null,
"abbreviation": null,
"value": "2021-05-04T00:00:00",
"measurement_unit": "date",
"decimal_places": null,
"is_critical": false
},
{
"key": "AVERAGE_DATE_OUTPUT",
"name": null,
"abbreviation": null,
"value": "2021-05-04T00:00:00",
"measurement_unit": "date",
"decimal_places": null,
"is_critical": false
},
This is my JSON structure, I want to extract the objects inside my Performance Array. So I made a map and pushed the data to my Hook, now I'm trying to map inside my Performance array and push the objects to my hook, later I'll render in a table.