2017-02-09 49 views
2

我有以下的反應,終極版狀態:Redux的狀態更好的結構

{ 
    response: { 
    json: [], 
    waitingForResponse: false, 
    communicationError: false, 
    searchButtonDisabled: true 
    }, 
    routing: { 
     ... 
    } 
    } 
} 

我想有一個狀態更好的結構是這樣的:

{ 
    app: { 
     home: { 
      searchButtonDisabled: true 
     }, 
     warning: { 
      waitingForResponse: false, 
      communicationError: false 
     }, 
     rest: { 
      json: [], 
      httpStatus: 200 
     } 
    }, 

    routing: ... 
} 

我想我需要與商店定義做一些魔術。我現在App.js文件看起來像這樣

const store = createStore(
    combineReducers({ 
     response: reducer, 
     routing: routerReducer 
    }), 
    composeEnhancers(applyMiddleware(thunk)) 
); 

是否有意義建立這個結構或者它只是讓我的代碼更復雜,沒有任何額外的好處?

如果它不是一個好主意,然後我就可以在原有屬性的名稱前加上前綴部分:

{ 
    response: { 
    restJson: [], 
    restHttpStatus: 200, 

    warningWaitingForResponse: false, 
    warningCommunicationError: false, 

    homeSearchButtonDisabled: true 
    }, 
    routing: { 
    locationBeforeTransitions: { 
     pathname: '/hello/', 
     ... 
    } 
    } 
} 

什麼是建立一個更大的狀態組件的最佳做法?

回答

3

從長遠來看,規範化(儘可能保持平坦)將使您的生活更輕鬆,儘管沒有硬性規定。

一下官方的文檔: http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html

馬克埃裏克森寫的好東西一噸的它已經: https://hashnode.com/post/what-are-the-best-practices-when-normalizing-redux-data-cive6wc8b08aj3853mvjpfsh1

+0

驚人的文檔!謝謝克里斯。 – zappee

+1

不用擔心,所有功勞都歸馬克所有,他是一支槍 – Chris

+0

呵呵,謝謝!很高興看到我寫的東西對人們有用:) – markerikson