rerender component on button click react Hooks

Viewed 942

I am new to the react world, all I wants to re-render the DOM once onClick={() => deleteHandler(user) is triggered, so that the list of users should get updated. Right now it only updates when the page is refreshed. as suggested in some of the answers here I also tried to setState but no luck. Can anyone please have a look and confirm where am I going wrong.

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);

PS: Already checked the answers for the below questions.

Can you force a React component to rerender without calling setState?

React : rerender component when click a button

1 Answers

This right here is not going to do anything because dispatch is a void function (unless you are using thunk).

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

connect subscribes to changes in the Redux store. It will re-render your component with updated props every time that the value of state.userList changes. This should happen as a result of calling dispatch(deleteUser(user._id)). If it's not happening, then the problem and the solution are in your reducer rather than in this component.

If I have to guess, I suspect that you are calling a mutating function on state.userList such as splice.

Related