我正在尝试在删除请求后刷新状态。正在删除正确的id,但它根本没有刷新状态,因为操作负载为空。下面是我当前的代码。
actions\index.js
...
function deleteCustomerSucceeded(customer){
return{
type:"DELETE_CUSTOMER_SUCCEEDED",
payload:{
customer
}
}
}
export function deleteCustomer(id){
return(dispatch) =>{
api.deleteCustomer(id).then((res) => {
dispatch(deleteCustomerSucceeded(res.data))
})
}
...
减速器\index.js
...
case 'DELETE_CUSTOMER_SUCCEEDED':{
return{
...state,
customers: state.customers.filter(customer => customer.id !== action.payload)
}
}
...
api\index.js
export function deleteCustomer(id){
return client.delete(`/customers/${id}`)
}
myview.js
deleteCustomer(){
let state = {submitted: true}
this.props.onDeleteCustomer(this.state.customer.id)
state = {
...state,
customer: this.emptyCustomer,
deleteCustomerDialog: false
}
this.setState(state)
}
如果需要任何进一步的澄清或任何额外的代码来帮助回答这个问题,请告诉我。
Kapobajza回答后,我对代码进行了这些更改以使其正常工作。
减速器\index.js
case 'DELETE_CUSTOMER_SUCCEEDED':{
return{
...state,
customers: state.customers.filter(customer => customer.id !== action.payload.id)
}
}
actions\index.js
function deleteCustomerSucceeded(id){
return{
type:"DELETE_CUSTOMER_SUCCEEDED",
payload:{
id
}
}
}
export function deleteCustomer(id){
return(dispatch) =>{
api.deleteCustomer(id).then((res) => {
dispatch(deleteCustomerSucceeded(id))
})
}
}