<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看