在 ReactJS 上加载组件时调用函数时出现问题。我尝试使用 componentDidMount() 并且编译时出错。请在下面检查我的代码。谢谢
export default function Customers() {
const classes = useStyles();
const searchBarStyles = useStyles2();
const [page, setPage] = React.useState(0);
const [rowsPerPage, setRowsPerPage] = React.useState(10);
const dispatch = useDispatch();
const handleChangePage = (event, newPage) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (event) => {
setRowsPerPage(+event.target.value);
setPage(0);
};
const fetch = () => {
dispatch(fetchPendingAdmissions());
};
componentDidMount() {
fetch()
}
return (
<div>
<h1 className={classes.h1}>Customers</h1>
<Paper component="form" className={searchBarStyles.root}>
<InputBase
className={searchBarStyles.input}
placeholder="Search..."
inputProps={{ 'aria-label': 'search...' }}
/>
<IconButton type="submit" className={searchBarStyles.iconButton} aria-label="search">
<SearchIcon />
</IconButton>
</Paper>
<Paper className={classes.root}>
<TableContainer className={classes.container}>
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
<TableCell>Name</TableCell>
<TableCell>ID</TableCell>
<TableCell>Order Date</TableCell>
<TableCell>Actions</TableCell>
<TableCell></TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>Robert</TableCell>
<TableCell>1324171354</TableCell>
<TableCell>07-21-20</TableCell>
<TableCell>
<Button onClick={fetch} variant="contained" color="primary" startIcon={<VisibilityIcon />}>
View
</Button>
</TableCell>
<TableCell>
<Button variant="contained" className={classes.buttonSuccess} startIcon={<ThumbUpIcon />}>
Approve
</Button>
<Button variant="contained" className={classes.buttonError} startIcon={<CancelIcon />}>
Decline
</Button>
</TableCell>
</TableRow>
</TableBody>
</Table>
</TableContainer>
<TablePagination
rowsPerPageOptions={[10, 25, 100]}
component="div"
count={10}
rowsPerPage={rowsPerPage}
page={page}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
/>
</Paper>
</div>
);
}