# Redux 常見問題:Reducer
## 目錄
- [如何在 reducer 之間共享 state? combineReducers 是必須的嗎?](#reducers-share-state)
- [處理 action 必須用 switch 語句嗎?](#reducers-use-switch)
## Reducer
<a id="reducers-share-state"></a>
### 如何在 reducer 之間共享 state? `combineReducers` 是必須的嗎?
Redux store 推薦的結構是將 state 對象按鍵值切分成 “層”(slice) 或者 “域”(domain),并提供獨立的 reducer 方法管理各自的數據層。就像 Flux 模式中的多個獨立 store 一樣, Redux 為此還提供了 [`combineReducers`](/docs/api/combineReducers.md) 工具來簡化該模型。應當注意的是, `combineReducers` **不是** 必須的,它僅僅是通過簡單的 JavaScript 對象作為數據,讓 state 層能與 reducer 一一關聯的函數而已。
許多用戶想在 reducer 之間共享數據,但是 `combineReducers` 不允許此種行為。有許多可用的辦法:
- 如果一個 reducer 想獲取其它 state 層的數據,往往意味著 state 樹需要重構,需要讓單獨的 reducer 處理更多的數據。
- 你可能需要自定義方法去處理這些 action,用自定義的頂層 reducer 方法替換 `combineReducers`。你可以使用類似于 [reduce-reducers](https://github.com/acdlite/reduce-reducers) 的工具運行 `combineReducers` 去處理盡可能多的 action,同時還要為存在 state 交叉部分的若干 action 執行更專用的 reducer。
- 類似于 `redux-thunk` 的 [異步 action 創建函數](/docs/advanced/AsyncActions.md) 能通過 `getState()` 方法獲取所有的 state。 action 創建函數能從 state 中檢索到額外的數據并傳入 action,所以 reducer 有足夠的信息去更新所維護的 state 層。
只需牢記 reducer 僅僅是函數,可以隨心所欲的進行劃分和組合,而且也推薦將其分解成更小、可復用的函數 (“reducer 合成”)。按照這種做法,如果子 reducer 需要一些參數時,可以從父 reducer 傳入。你只需要確保他們遵循 reducer 的基本準則: `(state, action) => newState`,并且以不可變的方式更新 state,而不是直接修改 state。
#### 補充資料
**文檔**
- [API: combineReducers](/docs/api/combineReducers.md)
- [Recipes: Reducers 基礎結構](/docs/recipes/StructuringReducers.md)
**討論**
- [#601: A concern on combineReducers, when an action is related to multiple reducers](https://github.com/reactjs/redux/issues/601)
- [#1400: Is passing top-level state object to branch reducer an anti-pattern?](https://github.com/reactjs/redux/issues/1400)
- [Stack Overflow: Accessing other parts of the state when using combined reducers?](http://stackoverflow.com/questions/34333979/accessing-other-parts-of-the-state-when-using-combined-reducers)
- [Stack Overflow: Reducing an entire subtree with redux combineReducers](http://stackoverflow.com/questions/34427851/reducing-an-entire-subtree-with-redux-combinereducers)
- [Sharing State Between Redux Reducers](https://invalidpatent.wordpress.com/2016/02/18/sharing-state-between-redux-reducers/)
<a id="reducers-use-switch"></a>
### 處理 action 必須用 `switch` 語句嗎?
不是。在 reducer 里面你可以使用任何方法響應 action。 `switch` 語句是最常用的方式,當然你也可以用 `if`、功能查找表、創建抽象函數等。事實上,雖然 Redux 要求每個 action 對象都有一個 `type` 的字段,但是你的 reducer 邏輯不必一定要依賴它做處理。也就是說,標準方法肯定是用基于 `type` 的 `switch` 語句或者查找表。
#### 補充資料
**文檔**
- [Recipes: Reducing Boilerplate](recipes/ReducingBoilerplate.md)
**討論**
- [#883: take away the huge switch block](https://github.com/reactjs/redux/issues/883)
- [#1167: Reducer without switch](https://github.com/reactjs/redux/issues/1167)
- 自述
- 介紹
- 動機
- 核心概念
- 三大原則
- 先前技術
- 學習資源
- 生態系統
- 示例
- 基礎
- Action
- Reducer
- Store
- 數據流
- 搭配 React
- 示例:Todo List
- 高級
- 異步 Action
- 異步數據流
- Middleware
- 搭配 React Router
- 示例:Reddit API
- 下一步
- 技巧
- 配置 Store
- 遷移到 Redux
- 使用對象展開運算符
- 減少樣板代碼
- 服務端渲染
- 編寫測試
- 計算衍生數據
- 實現撤銷重做
- 子應用隔離
- 組織 Reducer
- Reducer 基礎概念
- Reducer 基礎結構
- Reducer 邏輯拆分
- Reducer 重構示例
- combineReducers 用法
- combineReducers 進階
- State 范式化
- 管理范式化數據
- Reducer 邏輯復用
- 不可變更新模式
- 初始化 State
- 結合 Immutable.JS 使用 Redux
- 常見問題
- 綜合
- Reducer
- 組織 State
- 創建 Store
- Action
- 不可變數據
- 代碼結構
- 性能
- 設計哲學
- React Redux
- 其它
- 排錯
- 詞匯表
- API 文檔
- createStore
- Store
- combineReducers
- applyMiddleware
- bindActionCreators
- compose
- react-redux 文檔
- API
- 排錯