我有一个包含各种类型博客文章的减速器。我正在使用 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 分支也会随着我返回一个新副本而发生变化。因此,我将编写的任何选择器最终都会在每次过滤器更改时重新计算所有内容。
创建过滤状态选择器的标准做法是什么?