CRUD Delete all users

Whenever i start the project all users are deleted without the click in the start.

Function

deleteAluno = async (aluno) => {
    await axios
        .delete(url + "api/alunos/" + aluno)
        .then((response) => {
            this.getUsers();
        });
}

HTML/JSX:

{
    this.state.alunos.map((aluno) => (
        <div key={aluno.id}>{aluno.nome}
            <button type="button" onClick={this.deleteAluno(aluno.id)}>[X]</button>
        </div>
    ))
}

Answer

Instead of passing a function to the onClick prop, you’re calling the function directly causing the delete requests to be sent during the render.

<button
  type="button"
  onClick={() => this.deleteAluno(aluno.id)}
>
  [X]
</button>

Leave a Reply

Your email address will not be published. Required fields are marked *