2017-02-09 47 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