如何在删除数据后自动刷新页面?所以我不会手动刷新页面来查看更改。如果您需要此信息,我正在使用 axios 和 js-server 进行数据操作。我有这个代码:
import { connect } from "react-redux";
import { FetchUserList, RemoveUser } from "../Redux/Action";
import { useEffect } from "react";
import { Link } from "react-router-dom";
import { toast } from "react-toastify";
const UserListing = (props) => {
useEffect(() => {
props.loadUser();
}, []);
const handleDelete = (code) => {
if (window.confirm("Do you want to remove?")) {
props.removeUser(code);
props.loadUser();
toast.success("User removed successfully.");
}
};
return props.user.loading ? (
<div>
<h2>Loading...</h2>
</div>
) : props.user.errMessage ? (
<div>
<h2>{props.user.errMessage}</h2>
</div>
) : (
<>
<div className="card">
<div className="card-header">
<Link to={"/user/add"} className="btn btn-success">
Add User [+]
</Link>
</div>
<div className="card-body">
<table className="table table-bordered table-dark">
<thead className="">
<tr>
<td>Code</td>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
<td>Role</td>
<td>Action</td>
</tr>
</thead>
<tbody>
{props.user.userList &&
props.user.userList.map((item) => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.name}</td>
<td>{item.email}</td>
<td>{item.phone}</td>
<td>{item.role}</td>
<td>
<Link
to={"/user/edit/" + item.id}
className="btn btn-primary"
>
Edit
</Link>
<button
onClick={() => {
handleDelete(item.id);
}}
className="btn btn-danger"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</>
);
};
const mapStateToProps = (state) => {
return {
user: state.user,
};
};
const mapDispatchToProps = (dispatch) => {
return {
loadUser: () => dispatch(FetchUserList()),
removeUser: (code) => dispatch(RemoveUser(code)),
};
};
export default connect(mapStateToProps, mapDispatchToProps)(UserListing);
我看到有些人用 useEffect 或 useSelector 解决了这个问题,并尝试做类似的事情,但仍然没有帮助
如果我帮助了您,您可以使用
location.reload();
或者您有疑问请在下面评论:
const handleDelete = (code) => {
if (window.confirm("Do you want to remove?")) {
props.removeUser(code);
props.loadUser();
toast.success("User removed successfully.");
location.reload();
}
};