Reactjs 映射表中的数据后获取空数组数据
基本上,我试图从axios获取数据,然后将数据映射到material ui表中,但唯一的问题是在控制台中获取未定义的映射,我获取的是空数组Reactjs 映射表中的数据后获取空数组数据,reactjs,axios,react-hooks,material-ui,Reactjs,Axios,React Hooks,Material Ui,基本上,我试图从axios获取数据,然后将数据映射到material ui表中,但唯一的问题是在控制台中获取未定义的映射,我获取的是空数组 export default function BasicTable() { const classes = useStyles(); const [form, setForm] = useState([]); useEffect(() => { axios .get("http://localhost:80
export default function BasicTable() {
const classes = useStyles();
const [form, setForm] = useState([]);
useEffect(() => {
axios
.get("http://localhost:8080/getForm")
.then((res) => setForm(res.data.form));
}, []);
return (
<TableContainer component={Paper}>
<Table className={classes.table} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell> Bil</TableCell>
<TableCell align="right">Sales Dist.</TableCell>
<TableCell align="right">Sales Office (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{form.map((p, index) => {
return (
<TableRow key={index}>
<StyledTableCell align="right">{p.sale}</StyledTableCell>
<StyledTableCell align="right">{p.district}</StyledTableCell>
<StyledTableCell align="right">{p.billing}</StyledTableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
);
}
导出默认函数BasicTable(){
const classes=useStyles();
const[form,setForm]=useState([]);
useffect(()=>{
axios
.get(“http://localhost:8080/getForm")
.然后((res)=>setForm(res.data.form));
}, []);
返回(
比尔
销售区。
销售办事处(g)
{form.map((p,index)=>{
返回(
{p.sale}
{p.区}
{p.billing}
);
})}
);
}
使用async和wait它工作正常通常在调用get-request时,我们应该处理wait以获取所有数据这里是我调用get-request的代码
const API = "http://localhost:8080/getForm";
useEffect(() => {
const fetchData = async () => {
const result = await axios(API);
setForm(result.data);
};
fetchData();
}, []);
您能检查API请求的响应吗?你得到了什么回应?