2

I'm using react hooks and i trying to send the value to function delete and to do function put

           <Fab aria-label="delete"
                      className={classes.fab}
                      value={vacation.id}
                      key={vacation.id}
                      onChange={setDeleteid(vacation.id)}
                      onClick={DeleteCard}>
                      <DeleteIcon />
                    </Fab>

this is the function

  const DeleteCard = (e) => {
e.preventDefault();

console.log('delete' + deleteid);
alert(deleteid)
const confirm = window.confirm(`You are sure you want to delete vacation number ?`);
if (confirm == true) {
await axios.delete(`http://localhost:4000/users/admin/delete/:${e}`)
} else {
  alert("You pressed Cancel!")
}

}

this is the my state

     //vacation
  const [vacation, setVacation] = useState([]);
  const [deleteid, setDeleteid] = useState();
  //putcard
  const [description, setDescription] = useState("");
  const [destination, setDestination] = useState("");
  const [fromDate, setFromDate] = useState("");
  const [toDate, setToDate] = useState("");
  const [price, setPrice] = useState("");

Apparently the id I'm trying to send to state doesn't come in because it records me undifind


UPDATE

This is my state

const [deleteid, setDeleteid] = useState('');

Here I want to send and it gives me undefined

  onChange={() => setDeleteid(value)}

1 Answer 1

1

It's because you're executing the function as the DOM renders (it runs the function once). To fix this, change your onChange event to an arrow function that calls your function, like this:

onChange={() => setDeleteid(vacation.id)}

This should do the trick, happy coding!

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I did that already but then he gives me the next problem >>>Too many re-renders. React limits the number of renders to prevent an infinite loop.
why are you using e instead of e.target.value ? Also, you must bind the value to something that you can set with the input, otherwise you wont be able to change it (change the value to setDeleteId state)
No it doesn't help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.