I have a state that has multiple users initial State of the reducer
const initialState = [
{
id: 1,
name:"thename",
lastName: "Lastname"
},
{
id: 2,
name:"thename",
lastName: "Lastname"
},
{
id: 3,
name:"thename",
lastName: "Lastname"
},
];
And a component that shows them in a table, and the table has the functionality to update this values. So the thing i want to do is to take this values, make a copy of them to the state of the component and update them with my inputs.
Copying:
const users= useSelector((state) => state.usersReducer);
const [userState, setUserState] = useState(users)
useEffect(() => {
setUserState(users); // if the users state from the redux grows
}, [applicants]);
Mapping to display the data:
{ usersState.map( user=>{
<input onChange={(e) => handleChange(user)} value={user.name} >
})}
and with this handleChange
const handleChange = (data) => {
setUserState((user) => {
return [...user.filter((x) => x.id !== data.id), data];
});
};
but when I type in the input nothing happen... Because I could have multiple users, i'm mapping through them, I did forms before while learning, and never had this problem where the input doesn't change at all. But never had to do something like this, so i'm in trouble.