我有一个显示帖子列表的 Redux 应用程序。状态或多或少是这样的:
{
posts: [
{id: 1, title: 'My Post'},
{id: 2, title: 'Also this one'},
{id: 3, title: 'Other Post'}
],
visible_post_ids: [1, 2]
}
每当我加载一些帖子时,我都会将它们添加到posts
,然后我会替换visible_post_ids
.
这是我加载帖子的动作创建者:
function loadPosts (filters) {
return function (dispatch, getState) {
return fetch(`/posts.json?filters=${filters}`)
.then((response) => response.json())
.then((posts) => {
dispatch(postsLoaded(posts)) // Will update `posts`
const postIds = posts.map((post) => post.id)
dispatch(updateVisiblePosts(postIds)) // Will update `visible_post_ids`
})
}
}
我的问题是:从一个 thunk 发送两个(或更多)事件是否符合习惯?或者我应该只派出一个并在各种减速器中处理它?