我正在从 jsonplaceholder API 获取数据到我的状态。如何使用该deleteContact()方法删除数据?我最大的挣扎是如何使deleteContact()方法正确。
这种方法有错吗?
class RemoveFromAPI extends Component {
state = {
users: []
}
componentDidMount() {
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const users = res.data;
this.setState({ users });
})
}
deleteContact () {
axios.delete(`https://jsonplaceholder.typicode.com/users/${id}`);
.then(res => {
const users = res.data;
this.setState({ users });
})
}
render() {
const {users} = this.state
return (
<div>
<ul>
{ this.state.users.map(user => <li>{user.name}</li>)}
</ul>
<button
onClick={deleteContact}
>
Remove
</button>
</div>
);
}
}
RemoveFromAPI.propTypes = {};
export default …Run Code Online (Sandbox Code Playgroud)