React-Redux:删除一个项目不会重新渲染数组
React-Redux: deleting an item does not re-render the array
我通过将邀请的 ID 传递到后端来删除邀请,这很有效。但是,我的 reducer 无法正常工作以重新呈现新的过滤后的邀请数组。当我刷新页面时,删除的邀请消失了。我做错了什么?
操作:
export function deleteInvitation(id) {
const user = JSON.parse(localStorage.getItem('user'));
console.log('now deleting id ', id);
return function(dispatch) {
axios
.delete(`${ROOT_URL}/invitation/`, {
headers: { authorization: user.token },
params: { id: id }
})
.then(response => {
console.log(id);
dispatch({
type: DELETE_INVITATION,
id
});
});
};
}
减速器:
export default function(state = {}, action) {
switch (action.type) {
case INVITATION_SUCCESS:
return { ...state, invited: true, error: {} };
case INVITATION_FAILURE:
return { ...state, invited: false, error: { invited: action.payload } };
case FETCH_INVITATIONS:
return { ...state, invitations: action.payload };
case DELETE_INVITATION:
return {
...state,
invitations: state.invitations.filter(_id => _id !== action.id)
};
default:
return state;
}
}
我在猜测 invitations
数组的结构...
在减速器中,过滤功能似乎不正确。该操作正在传递 id
属性,我猜这是 invitation
对象的 属性。但是过滤器函数正在过滤 state.invitations
中的对象,其中对象是 id
。也就是说,减速器正在做这样的事情:
const action = {id: 0}
const invitation = [{
_id: 0,
name: 'Name 0',
location: 'Location 0'
},
{
_id: 1,
name: 'Name 1',
location: 'Location 1'
},
{
_id: 2,
name: 'Name 2',
location: 'Location 2'
}
];
console.log(invitation.filter(_id => _id !== action.id));
这将 return 完整的原始数组,因为过滤器函数正在检查 action.id
(一个数字)与 invitation
(一个对象)的不等式。基本上:
{
_id: 0,
name: 'Name 0', !=== number
location: 'Location 0'
}
对于任何 num
and/or 任何 invitation
对象都将 return 为真,因此过滤器函数将 return state.invitations
中的每个项目.
要更正此问题,请将 invitation._id
与 action.id
进行比较,如下所示:
const action = {id: 0}
const invitation = [{
_id: 0,
name: 'Name 0',
location: 'Location 0'
},
{
_id: 1,
name: 'Name 1',
location: 'Location 1'
},
{
_id: 2,
name: 'Name 2',
location: 'Location 2'
}
];
console.log(invitation.filter(invitation => invitation._id !== action.id));
我通过将邀请的 ID 传递到后端来删除邀请,这很有效。但是,我的 reducer 无法正常工作以重新呈现新的过滤后的邀请数组。当我刷新页面时,删除的邀请消失了。我做错了什么?
操作:
export function deleteInvitation(id) {
const user = JSON.parse(localStorage.getItem('user'));
console.log('now deleting id ', id);
return function(dispatch) {
axios
.delete(`${ROOT_URL}/invitation/`, {
headers: { authorization: user.token },
params: { id: id }
})
.then(response => {
console.log(id);
dispatch({
type: DELETE_INVITATION,
id
});
});
};
}
减速器:
export default function(state = {}, action) {
switch (action.type) {
case INVITATION_SUCCESS:
return { ...state, invited: true, error: {} };
case INVITATION_FAILURE:
return { ...state, invited: false, error: { invited: action.payload } };
case FETCH_INVITATIONS:
return { ...state, invitations: action.payload };
case DELETE_INVITATION:
return {
...state,
invitations: state.invitations.filter(_id => _id !== action.id)
};
default:
return state;
}
}
我在猜测 invitations
数组的结构...
在减速器中,过滤功能似乎不正确。该操作正在传递 id
属性,我猜这是 invitation
对象的 属性。但是过滤器函数正在过滤 state.invitations
中的对象,其中对象是 id
。也就是说,减速器正在做这样的事情:
const action = {id: 0}
const invitation = [{
_id: 0,
name: 'Name 0',
location: 'Location 0'
},
{
_id: 1,
name: 'Name 1',
location: 'Location 1'
},
{
_id: 2,
name: 'Name 2',
location: 'Location 2'
}
];
console.log(invitation.filter(_id => _id !== action.id));
这将 return 完整的原始数组,因为过滤器函数正在检查 action.id
(一个数字)与 invitation
(一个对象)的不等式。基本上:
{
_id: 0,
name: 'Name 0', !=== number
location: 'Location 0'
}
对于任何 num
and/or 任何 invitation
对象都将 return 为真,因此过滤器函数将 return state.invitations
中的每个项目.
要更正此问题,请将 invitation._id
与 action.id
进行比较,如下所示:
const action = {id: 0}
const invitation = [{
_id: 0,
name: 'Name 0',
location: 'Location 0'
},
{
_id: 1,
name: 'Name 1',
location: 'Location 1'
},
{
_id: 2,
name: 'Name 2',
location: 'Location 2'
}
];
console.log(invitation.filter(invitation => invitation._id !== action.id));