重新发布组件上的按钮点击反应挂钩



我是react世界的新手,我只想在触发onClick={() => deleteHandler(user)后重新渲染DOM,以便更新用户列表。现在,它只在刷新页面时更新。正如这里的一些答案所暗示的那样,我也试图设置State,但没有成功。谁能看一下,确认一下我哪里错了吗。

UserManagment.js

import React, { useEffect, useState } from "react";
import { BrowserRouter } from "react-router-dom";
import { useDispatch, connect } from "react-redux";
import image from "../logo1.png";
import { userList, deleteUser } from "../Action/userAction";
function UserManagment(props) {
const dispatch = useDispatch();
const users = props?.userData?.userData?.data?.user;
const [state, setState] = useState([]);
useEffect(() => {
dispatch(userList());
return () => {
//
};
}, [dispatch]);
useEffect(() => {
if(props?.userData?.userData?.status === 200){
}
}, [props, props.state]);
const deleteHandler = (user) => {
console.log(state)
setState(dispatch(deleteUser(user._id)));
};
return (
<BrowserRouter>
<div>
<div>
<div className="container">
<div className="row">
<header className="clearfix">
<nav className="navbar navbar-default" style={{ boxShadow: "1px 1px 4px 3px #C2B8B8", padding: "10px" }}>
<div className="container">
<ul className="nav navbar-nav">
<li>
<a href className="navbar-brand" style={{ padding: "6px" }}>
<img className=" text-center" alt="Logo" src={image} style={{ height: "40px" }} />
</a>
</li>
<li style={{ fontWeight: "bold", paddingTop: "15px" }}>User Management</li>
</ul>
<ul className="nav navbar-nav navbar-right">
<li>
<div className="inset" style={{ marginRight: "20px" }}>
{/* <h4>{userInfo ? <Link to="/profile">{userInfo.name}</Link> : <Link to="/signin">Sign In</Link>}</h4> */}
</div>
</li>
</ul>
</div>
</nav>
</header>
</div>
</div>
<div className="container">
<div className="row">
<div className="col-md-12 " style={{ boxShadow: "1px 1px 4px 3px #C2B8B8", background: "#fff", padding: "15px 10px" }}>
<table className="table table-hover table-bordered ">
<thead style={{ background: "#4caf50", color: "#fff" }}>
<tr>
<th className="text-center">First Name</th>
<th className="text-center">Role</th>
<th className="text-center">Email</th>
<th className="text-center">Phone Number</th>
<th className="text-center">Delete</th>
</tr>
</thead>
{
<tbody>
{users?.map?.((user) => (
<tr key={user._id}>
<td>{user.name}</td>
<td>{user.role}</td>
<td>{user.email}</td>
<td>{user.phone}</td>
<td className="text-center text-danger">
<button onClick={() => deleteHandler(user)}>
<i className="glyphicon glyphicon-trash text-danger" />
</button>
</td>
</tr>
))}
</tbody>
}
</table>
</div>
</div>
</div>
</div>
</div>
</BrowserRouter>
);
}
function mapStateToProps(state) {
return {
userData: state.userList,
};
}
function mapDispatchToProps(dispatch) {}
export default connect(mapStateToProps, mapDispatchToProps)(UserManagment);

附言:已经检查了以下问题的答案。

你能在不调用setState的情况下强制React组件重新发送吗?

React:点击按钮时重新发送组件

这里的这个不会有任何作用,因为dispatchvoid函数(除非使用thunk(。

setState(dispatch(deleteUser(user._id)));

connect订阅Redux存储中的更改。每次state.userList的值更改时,它都会使用更新的道具重新渲染组件。这应该是调用dispatch(deleteUser(user._id))的结果。如果没有发生,那么问题和解决方案就在减速器中,而不是在这个组件中。

如果非要我猜测的话,我怀疑您正在调用state.userList上的一个变异函数,比如splice

相关内容

  • 没有找到相关文章

最新更新