【发布时间】:2018-11-10 09:15:54
【问题描述】:
即使在更新了 props 之后,Redux 也不会更新组件。我有一个数据,其中包含lists,每个列表都可以包含cards,也可以包含lists(如文件夹内的文件夹)。 cards 和 lists 基本上是一个对象数组。
这是一个示例数据-
"data": [
{
"name": "new list",
"id": 31,
"created_at": "2018-11-07 17:48:10+05:30",
"lists": [],
"cards": [
{
"name": "new call",
"id": 8,
"display_order": 0,
"due_at": null,
"user_list_id": 31,
"description": null,
"created_at": "2018-11-08T16:34:14+05:30",
"labels": null
},
{
"name": "testing",
"id": 9,
"display_order": 2,
"due_at": null,
"user_list_id": 31,
"description": null,
"created_at": "2018-11-08T20:07:12+05:30",
"labels": null
},
{
"name": "new card",
"id": 7,
"display_order": 3,
"due_at": null,
"user_list_id": 31,
"description": null,
"created_at": "2018-11-08T16:34:09+05:30",
"labels": null
}
]
},
{
"name": "rename",
"id": 29,
"created_at": "2018-11-04 20:03:54+05:30",
"lists": [],
"cards": [
{
"name": "testing",
"id": 1,
"display_order": 0,
"due_at": null,
"user_list_id": 29,
"description": null,
"created_at": "2018-11-08T16:23:40+05:30",
"labels": null
},
{
"name": "testing again",
"id": 2,
"display_order": 1,
"due_at": null,
"user_list_id": 29,
"description": null,
"created_at": "2018-11-08T16:23:45+05:30",
"labels": null
}
]
}
]
现在,在我删除一张卡片后,我会像这样从列表中删除这张卡片。
const lists = this.props.lists;
const foundAt = lists.findIndex(e => parseFloat(e.id) === parseFloat(listId));
let cards = lists[foundAt]['cards'];
const cardFoundAt = cards.findIndex(e => parseFloat(e.id) === parseFloat(cardId));
cards.splice(cardFoundAt, 1);
lists[foundAt]['cards'] = cards;
this.props.rearrangeList(lists);
在我的行动中,我有这个-
export const rearrangeLists = (lists) => {
return {
type: REARRANGED_LISTS,
payload: lists
};
};
在我的减速器中,我有这个-
...
case REARRANGED_LISTS:
return {
...state,
lists: action.payload
};
...
我不知道我在哪里做错了。呈现列表的组件不显示更新版本,但在component 的render 函数中,当我执行console.log(this.props.lists) 时,我看到它显示的是更新版本,但在页面上却没有不要更新它。我真的很无知。请帮忙。提前感谢您的帮助。
【问题讨论】:
标签: reactjs redux react-redux redux-thunk