I'm trying to get a list of array items from an api response using useEffect, as a result I'm getting a list of objects. I want to convert that using Objects.key() method, but when I iterate through it, values won't be printed on view page. The response keeps on returning the call from api response.
API CALL
export function getServicesById(facility_id) {
return axios
.get(`${baseURL}patients/appointments/1`)
.then((response) => {
if(response.status===200){
// console.log(response.data)
return response
}
}).catch(error => {
console.log('error', error)
return error
});
}
useEffect to get response data
useEffect(() => {
getServicesById()
.then((res) => {
setResponseData(res.data);
})
.catch((error) => {
console.log(error);
});
// console.log('data is', newResponseData);
}, [responseData, setResponseData]);
Converting objects to array items
const newResponseData = Object.keys(responseData).reduce((array, key) => {
return [...array, { key: responseData[key] }];
}, []);
Jsx page where I'm rendering the data
<TableBody>
{newResponseData &&
newResponseData.map((serv, index) => (
<TableRow key={index}>
<TableCell align="left" data-label="Action">
<Field
name="status"
component={renderSelectInput}
variant="outlined"
size="small"
className="fileds"
inputClass="sm-filed"
>
<MenuItem selectedValue="progress">
In progress
</MenuItem>
<MenuItem value="closed">
Closed
</MenuItem>
</Field>
</TableCell>
<TableCell data-label="Id">{serv.id}</TableCell>
<TableCell data-label="Patient name">{serv.patient_name}</TableCell>
<TableCell data-label="Test Name">{serv.test_name}</TableCell>
</TableRow>
))}
</TableBody>
newResponseDataarray. What isn't working? Can you describe that in a bit more detail? Potential issue I see is the mapped objects have only akeykey.