CRUD删除所有用户



每当我启动项目时,所有用户都会被删除,而不会在启动时单击。

功能

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>
))
}

您不是将函数传递给onClick道具,而是直接调用该函数,从而在渲染期间发送删除请求。

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

最新更新