I have json array as below -
[{"sup_Id":6,"sup_ShortCode":"A"},{"sup_Id":7,"sup_ShortCode":"B"},{"sup_Id":8,"sup_ShortCode":"C"},{"sup_Id":1000,"sup_ShortCode":"D"}]
React component reading this array as -
import React,{useEffect,useState} from 'react'
import axios from 'axios';
function AllSuppliers() {
const [suppliers, setstate] = useState([])
useEffect(() => {
// GET request using axios inside useEffect React hook
axios.get('http://localhost:62815/api/values/GetAllSuppliers')
.then(x => setstate({suppliers:x.data}))
.catch(error => {
alert(error);
});;
}, []);
return (
<>
<table style={{width: '50%'}}>
<thead>
<tr>
<th>
Supplier Id
</th>
<th>
Supplier Name
</th>
</tr>
</thead>
{
suppliers.map((supplier)=>supplier)
}
</table>
</>
)
}
export default AllSuppliers
I am getting -
TypeError: suppliers.map is not a function
error
I also tried with - suppliers[0].map since its an array. But this also did not worked.
GetAllSuppliersrequest?setstatefunction instead of pass an objectsetstate(JSON.parse(x.data))