在链接中:Task-Based Updates 我不明白下面的代码:
import posts from "./postsReducer"; // missing this code??
import comments from "./commentsReducer"; // missing this code??
为什么要这样做?
const combinedReducer = combineReducers({
posts,
comments
});
const rootReducer = reduceReducers(
combinedReducer,
featureReducers
);
只有featureReducers好吗?不需要combindedReducer?什么是postsReducer代码,commentsReducer代码?
感谢您的帮助!
答案 0 :(得分:0)
不幸的是,这个例子有点令人困惑(通常可靠的Redux文档中的少数几个地方之一)。如果您转到here并查看“第三种方法”,您会发现这个概念的解释要好一些。
基本上,postReducer
和commentReducer
可以处理仅修改posts
或仅修改comments
的操作 - 也就是说,不需要更改多个表的操作(例如posts
AND comments
)。 featureReducer
是基于任务的reducer,它处理需要更新多个表的操作。
一个非常简单的例子:
const postReducer = (state = {}, action) => {
return state
}
const commentReducer = (state = {}, action) => {
return state
}
const combined = combineReducers({
posts: postReducer,
comments: commentReducer
})
const createComment = (state, action) => {
switch(action.type) {
case 'CREATE_COMMENT':
// update multiple tables (linked example actually does an ok job ok demonstrating this)
return updatedState
default:
return state;
}
}
const rootReducer = reduceReducers( combined, createComment )
在此示例中,前两个Reducer只是创建状态形状。第三个,它取决于前两个为它设置状态,更新redux存储中的多个表。
你的状态将如下所示:
{
posts: {},
comments: {}
}
如果您对reduceReducers
感到困惑,请尝试将其视为combineReducers
,只是它不会影响状态。