javascript - 在 React JS 上删除后页面不刷新(需要手动重新加载)
问题描述
我是 React JS 的新手。我正在尝试使用 React 和 Spring 构建一个全栈 CRUD 应用程序。
我已经正确配置了 Delete REST API(在 Postman 上测试),它工作得很好。但是,当我单击页面上的删除按钮时,该条目不会自动从页面中删除。我必须执行手册页重新加载/刷新。重新加载页面后,不再显示该条目(已删除)。
可能是什么问题?
客户服务.js
deleteCustomer(customerId) {
return axios.delete(customer_base_url + '/' + customerId);
}
列出客户组件构造函数
class ListCustomerComponent extends Component {
constructor(props){
super(props)
this.state = {
customers: []
}
this.addCustomer = this.addCustomer.bind(this);
this.editCustomer = this.editCustomer.bind(this);
this.deleteCustomer = this.deleteCustomer.bind(this);
this.viewCustomer = this.viewCustomer.bind(this);
}
editCustomer(id) {
this.props.history.push(`/add-customer/${id}`);
}
deleteCustomer(id) {
CustomerService.deleteCustomer(id).then( res => {
this.setState({customers: this.state.customers.filter(customer => customer.id !== id)});
});
}
删除按钮
<button style = {{ marginLeft: "10px"}} onClick = { () => this.deleteCustomer(customers.customerId)} className = "btn btn-danger"> Delete </button>
解决方案
在您的删除功能中,我注意到您使用了.customerId
.
this.deleteCustomer(customers.customerId)}
但是在您的过滤器功能中,您正在检查.id
this.state.customers.filter(customer => customer.id !== id)}
所以你的过滤功能应该是
this.state.customers.filter(customer => customer.customerId !== id)}
假设 .customerId 反映了客户的唯一标识符。
推荐阅读
- c++ - 无法将 boost 库与 g++ Windows 链接
- android - Rxjava,如何阻塞直到它完成?
- java - 使用算法验证时,令牌的签名无效:HmacSHA256
- google-apps-script - 在 webapp html 文件中使用 Apps 脚本库?
- php - Codeigniter:如何上传多个文件(不是 input[] 数组)并返回文件的当前路径
- node.js - 我收到'TypeError:无法读取未定义的属性'数据''
- arm - PCIe 总线上的写入是原子的吗?
- python - 如何使用 python 从 web 抓取创建 pandas 数据框
- android - 如何从 PlayStore/AppStore 获取应用程序最新版本反应原生
- ios - 如何在不丢失 exif 数据的情况下将 UIImage 转换为 JPEG?