0
EDIT: I have a crud in React, and in it one of the buttons is to delete a user. When I click delete, open a modal to confirm, below the button, sending the id to be deleted:
<button className="custom-button"
onClick={event => openModalDelete(event, user.id)}
value={user.id} style={{ marginLeft: "6%" }}>Deletar
</button>
here follows the code of the function that opens the modal, and in this case printa the received id correctly, from the selected user, the modal appears and everything ok.
const openModalDelete = (e, id) => {
e.preventDefault();
console.log(id, 'id recebido');
showModal = !showModal;
const element = document.getElementById('modal');
setTimeout(() => {
if (showModal) {
element.classList.add('modal-show')
}
else {
element.classList.remove('modal-show')
}
}, 250);
}
So far ok: But then to delete you need to confirm. By clicking confirm, I do not know how to receive the id. I tried with e.target.value but nothing comes in.
const handleDelete = async (e) => {
console.log("deleting", e);
//const deletedId = await e.target.value;
//await api.delete(`/users/${deletedId}`);
//window.location.reload();
}
Now here is the modal:
<div id="modal">
<div >
<div className="container">
<a href="/" onClick={event => openModalDelete(event)} style={{ color: "black" }}>x</a>
<div style={styles.alignCenter}>
Deseja realmente deletar?
</div>
<button style={styles.alignCenter} className="custom-button"
onClick={event => handleDelete(event)}>Deletar</button>
</div>
</div>
</div>
In this Event q goes on handleDelete, it goes empty.
I ended up solving here doing a global Let, which receives where he sees the id (when he opens the modal) and keeps, and then this one he can see
But still, I don’t know if it’s the right way to pass on the id. Any ideas? About deleting and updating the page? This shouldn’t be good practice, right?
It’s my first post here, if you need more code, please let me know. Thank you.
Hello Willian. Can’t pass id as parameter to your handleDelete function? Where is function call?
– Carlos Querioz
Your question is missing a lot for us to answer, I’ve seen that there are things wrong with your code so if you can improve your question and show what really isn’t happening?
– novic
@novic mals, I thought it would be too extensive to put everything here.
– Willian Cozzi Candido
@Carlosquerioz he calls in onClick, I edited the post, now maybe I’ve been clearer... thanks!
– Willian Cozzi Candido
Your code has imperfections, you are learning to mess with React?
– novic
@novic yes! I’m learning! I did some projects but all small, and nothing too complex...
– Willian Cozzi Candido