0

我有一个包含各种类型博客文章的减速器。我正在使用 select 来获取我需要的部分数据,但每次状态更改时 reselect 都会重新计算所有选择器。

initialstate = {
  blogPosts: { byId: { 1: { id: 1, parent: null } }, ids: [1] },
  commentPosts: { byId: { 2: { id: 2, parent: 1 } }, ids: [2] },
  filter:{ selectedBlog: null }
};

const reducer = (state = initialstate, action) => {
  if (action.type == 'filter') {
    return object.assign(state, { filter: action.filter });
  }
  return state;
}

const Blogselector = state => state.posts.blogPosts
const Commentselector = state => state.posts.commentPosts
const getFilters = state => state.posts.filter;

在我的组件中,当我显示博客文章时,我调度了设置的操作filter.selectedBlog = post.id

问题是当过滤器发生变化时,状态的整个 Posts 分支也会随着我返回一个新副本而发生变化。因此,我将编写的任何选择器最终都会在每次过滤器更改时重新计算所有内容。

创建过滤状态选择器的标准做法是什么?

4

1 回答 1

0

请告诉我您的选择器代码部分。

在您上面的代码中,已filter更改但未更改,因此不需要进行不必要的重新计算。blogPostscommentPosts

于 2017-04-26T04:47:54.407 回答