2017-04-07 73 views
0

在链接: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 
); 

only featureReeducers okie?不需要combindedReducer? anh什么是postsReducer代码,commentsReducer代码?

感谢您的帮助!

+0

'postsReducer' and'commentsReducer' are here:http://redux.js.org/docs/recipes/reducers/UpdatingNormalizedData.html#slice-reducer-composition – CodinCat

+0

但是,在该链接中,评论是: '// redurs/posts.js' '// redurs/comments.js' so,import should: 'import postsReducer from“./posts”; //不从“./postsReducer”导入帖子;' '从“./comments”导入commentsReducer; // not import comments from“./commentsReducer”;' 和其他问题,为什么需要'reduceReducers(combinedReducer,featureReducers)'?处理redux状态树两次?一个与combinedReducer组合,然后继续使用featureReducers? 感谢您的回复! – CuongNT

回答

0

不幸的是,这个例子有点令人困惑(正常Redux文档中为数不多的地方之一)。如果你去here并查看'第三种方法',你会看到这个概念更好一点解释。

从本质上讲,postReducercommentReducer在那里处理仅修改posts或仅comments凹口 - 即行动,事情不需要到多个表的变化(例如postscomments)。 featureReducer是基于任务的简化器,用于处理需要更新多个表的操作。

一个非常简化的例子:

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) 

在这个例子中,前两个减速器刚创建的状态的形状。第三个依赖于前两个设置状态,在redux商店中更新多个表。

你的状态形状将是这样的:

{ 
    posts: {}, 
    comments: {} 
} 

如果你感到困惑reduceReducers,只是尽量把它想combineReducers,只是它不影响状态的形状。