2016-04-01 42 views
4

終極版文檔建議使用normalizr設計的狀態是這樣的形狀:你可以使用組合減速器組成深層狀態減速器嗎?

{ 
    entities: { 
    cards: { 
     42: { 
     id: 42, 
     text: 'Hello', 
     category: 2 
     }, 
     43: { 
     id: 53, 
     text: 'There?', 
     category: 1 
     }, 
    } 
    categories: { 
     1: { 
     id: 1, 
     name: 'Questions' 
     } 
     2: { 
     id: 2, 
     name: 'Greetings' 
     }, 
    } 
    }, 
    filter: 'SHOW_ALL', 
    allCardsList: { 
    isFetching: false, 
    items: [ 42, 43 ] 
    }, 
} 

當然,這會分裂成三個組合的減速器(filterallThingsListentities),但是,我會在我看來,想要爲entities.cardsentities.categories編寫單獨的縮減器。

是有辦法的實體的管理分成subreducers,將允許這樣的成分:

let rootReducer = combineReducers({ 
    entities: { 
     things, 
     categories 
    }, 
    filter, 
    allCardsList 
}); 

是否有任何優勢,保持在entitiescardscategories,而不是保持根目錄下(這將允許使用combineReducers)?

{ 
    cards: { ... }, 
    categories: { ... }, 
    filter: 'SHOW_ALL', 
    allCardsList: { ... } 
} 

回答

6

是有辦法的實體的管理分成subreducers,將允許這樣的成分?

當然! combineReducers()只是給你一個減速,所以你可以用它幾次:

let rootReducer = combineReducers({ 
    entities: combineReducers({ 
     things, 
     categories 
    }), 
    filter, 
    allCardsList 
}); 

shopping-cart example in Redux repo演示了這個方法。

在實體中保留卡和類別有什麼好處,而不是保留在根級上嗎?

這取決於你,但我覺得你建議的結構更容易處理。在一個密鑰下對所有實體進行分組確實更容易,並且可以使用combineReducers(),如上所示。

+0

現在看來很明顯,如果我想到'combineReducers'的實現。謝謝! – c10b10