2016-12-28 68 views
0

我试图用一个例子来研究redux流程。但是陷入了两者之间。这里是相同的plunkr链接。还原状态更新不正确

function combineReducers(currentState, action) { 

    var nextState = Object.assign({}, currentState); 

    /*On load placeholder details for all thumbnails*/ 
    var placeholder = { 
     urlPath: "http://placehold.it/640x480", 
     header: "PLACEHOLDER", 
     description: "Description text for the above image" 
    }; 
    if (currentState === undefined) {  
     nextState = placeholder; 
     return nextState; 
    } 
    //Problem here i guess 
    nextState = { 
     animals : animalReducer(nextState.animals, action),  
     architecture : architectureReducer(nextState.architecture, action)  
    } 

    return nextState; 
} 

应用程序加载初始状态为将所有媒体元素设置为占位符。 (这是工作) 在个别按钮点击,它应该获取每个类别的细节,只填充这些媒体元素。

问题:

当我点击执行按钮,或1层2的元件正在更新 在一起。理想的情况是我期待在4

我还没有实现3和4 点击元素1,元素2架构的细节,自然得到3 人们只动物细节,我相信,如果这个作品,那么它将更多地为每个状态添加额外的动作和缩减器。

我觉得问题出在rootReducer.js第19行或者index.js第34或37行,但不知道如何继续!任何指针都会有很大的帮助!今天我头上已经掉了很多头发!

PS:我知道的jQuery做的是一种污物的,但只是为了学习的目的!

高级感谢帮助者! 干杯。

回答

1

https://plnkr.co/edit/WDyQHy5tftm2EX6AFQ9j?p=preview

var defaultState = { 
    animals: Object.assign({}, placeholder), 
    architecture: Object.assign({}, placeholder) 
}; 

if (currentState === undefined) {  
    nextState = defaultState; 
    return nextState; 
} 
nextState = { 
    animals : animalReducer(nextState.animals, action),  
    architecture : architectureReducer(nextState.architecture, action)  
} 
  1. 的减速没有在默认情况下返回原来的状态

  2. 默认状态格式,并将合并的减速状态格式是不同的

+0

感谢您的工作!确实有帮助 –

1

在这两个animalReducerarchitectureReducer你需要在default:情况下返回currentState,否则你最好null另一部分,每次有新的变化。 nextState未在default:中定义。

减速机默认不会对状态做任何事情,它必须保持不变。只有存在匹配的操作时,它才会创建一个具有更新状态的新对象。这里的事情是你不遵守这个规则,并且偶然地将状态默认为空。

+0

@ donmatrin,感谢您的解释!作为有用的......更新后的plunk也有帮助!感谢您的快速转身, –