Handle delete works but does not render the component at '/'
after the item is deleted. Typing in that url will direct to the correct component.
I also want the handleEdit
to also be referred by its link (/Job/Edit#, where # is rowData.id) instead of being directly called.
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}
/>
)}
</>
);
}
DataDelete:
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!')
Returning Link
under no circumstances will trigger navigation. Technically you can return Redirect , but with your structure it wouldn't help because the returned element isn't being rendered. Try instead usinghistory to navigate by pushing the route "/"
to history:
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("/");
}
// ...
}
Otherwise if you restructure component to conditionally render <Redirect to="/" >
based on new state property that is set after delete is successful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.