【发布时间】:2018-03-16 17:33:15
【问题描述】:
我有一个使用 Redux 的反应应用程序。现在我有一个与 redux 一起显示的书籍列表,并希望对其实施 CRUD。列出书籍的代码是:
listBooks(){
return this.props.books.map((books) => {
return(
<tbody key={books.title}>
<tr className="tr-book">
<td>{books.series}</td>
<td>{books.title}</td>
<td>Vol. {books.volume}</td>
<td>
<button onClick={() => deleteBook(this.props.books, books.id)}className="btn-action">Delete</button>
<button onClick={() => editBook(this.props.books)}className="btn-action">Update</button>
</td>
</tr>
</tbody>
);
})
}
而且它列出的很好。 deleteBook 动作有这个动作:
export function deleteBook (book, id) {
book = book.filter(function(book){
return book.id !== id
});
console.log(book.id);
return {
type: "BOOK_DELETED",
payload: book
}
}
而且它不起作用。我尝试了一些方法,但大多数都不起作用,因为 book 不是数组,而是数组对象。在这种情况下,我如何告诉函数deleteBook 过滤这些书并只返回book.id !== id 的那些?
更新:这里是放置书籍的地方:
export default function listBooks() {
return[
{
id: 1,
volume: 1,
series: 'A Song of Ice and Fire',
title: 'Game of Thrones',
rank: 0
},
{
id: 2,
volume: 2,
series: 'A Song of Ice and Fire',
title: 'Clash of Kings',
rank: 0
},
{
id: 3,
volume: 3,
series: 'A Song of Ice and Fire',
title: 'Storm of Swords',
rank: 0
},
{
id: 4,
volume: 4,
series: 'A Song of Ice and Fire',
title: 'A Feast of Crows',
rank: 0
}, {
id: 5,
volume: 5,
series: 'A Song of Ice and Fire',
title: 'A Dance With Dragons',
rank: 0
}, {
id: 6,
volume: 1,
series: 'The Lord of the Rings',
title: 'The Fellowship of the Ring',
rank: 0
}, {
id: 7,
volume: 2,
series: 'The Lord of the Rings',
title: 'The Two Towers',
rank: 0
}, {
id: 8,
volume: 3,
series: 'The Lord of the Rings',
title: 'The Return of the King',
rank: 0
}
]
}
【问题讨论】:
-
书籍的结构是什么,不知道你说的数组对象是什么意思
-
同意上面的Shubham,书籍的初始状态是什么?它的数据结构是什么?
-
添加了一些新代码@ShubhamKhatri
-
该操作不应修改图书。你必须写一个reducer来做,当它把动作作为参数时
标签: javascript reactjs redux