简体   繁体   English

CRUD 删除所有用户

[英]CRUD Delete all users

Whenever i start the project all users are deleted without the click in the start.每当我启动项目时,所有用户都将被删除,而无需单击开始。

Function Function

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

HTML/JSX: HTML/JSX:

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

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.不是将 function 传递给onClick ,而是直接调用 function 导致在渲染期间发送删除请求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM