[英]React Router not rendering component on return
处理删除有效,但在删除项目后不会在'/'
处呈现组件。 输入该 url 将定向到正确的组件。
我还希望handleEdit
也可以通过其链接(/Job/Edit#,其中 # 是 rowData.id)引用,而不是直接调用。
import {Link} from 'react-router-dom';
export default function ListJobs(props) {
const url = 'http://localhost:8000/api/Jobs/'
const [data, loading] = DataLoader(url);
const handleEdit = (e,rowData) => {
return <EditJob id={rowData.id} />
}
const handleDelete = (e,rowData) => {
//edit operation
DataDelete(url, rowData.id)
return <Link to='/' />
}
const createButton =
<div style={{display: 'flex', justifyContent:'center', alignItems:'center'}}>
<Button
component={Link} to='/Job/Create'
variant="contained"
color="primary">
Create New Job
</Button>
</div>
return (
<> {loading ? (
<Grid
container
spacing={0}
alignItems="center"
justify="center"
style={{ minHeight: '90vh' }}
>
<CircularProgress size="10vh" />
</Grid>
) : (
<MuiTable
model="Job"
data={data}
url={url}
handleEdit={handleEdit}
handleDelete={handleDelete}
createButton={createButton}
/>
)}
</>
);
}
数据删除:
export function DataDelete(url, id) {
url = url + id + '/'
async function DeleteData() {
const options ={
method : 'DELETE',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
}
}
const response = await fetch(url, options)
.then(response =>{
console.log(response.status);
});
}
if (id === undefined)
alert('Job not found.')
else
DeleteData()
alert('Job Deleted!')
在任何情况下返回Link
都不会触发导航。 从技术上讲,您可以返回Redirect ,但对于您的结构,它无济于事,因为未呈现返回的元素。 尝试通过将路线"/"
推送到历史来使用历史进行导航:
import { useHistory } from "react-router-dom";
export default function ListJobs(props) {
let history = useHistory();
// ...
const handleDelete = (e,rowData) => {
//edit operation
DataDelete(url, rowData.id)
history.push("/");
}
// ...
}
否则,如果您重构组件以根据删除成功后设置的新状态属性有条件地呈现<Redirect to="/" >
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.