|
| 1 | +--- |
| 2 | +id: code-splitting |
| 3 | +title: Code Splitting |
| 4 | +sidebar_label: Code Splitting |
| 5 | +hide_title: true |
| 6 | +--- |
| 7 | + |
1 | 8 | # Code Splitting
|
2 | 9 | In large web applications, it is often desirable to split up the app code into multiple JS bundles that can be loaded on-demand. This strategy, called 'code splitting', helps to increase performance of your application by reducing the size of the initial JS payload that must be fetched.
|
3 | 10 |
|
4 |
| -To code split with Redux, we want to be able to dynamically add reducers to the store. The default usage of Redux mandates that a single root reducer should to be passed to the `configureStore` call, which makes dynamically adding new reducers more difficult. Below, we discuss some approaches to solving this problem and reference two libraries that provide this functionality. |
| 11 | +To code split with Redux, we want to be able to dynamically add reducers to the store. However, Redux really only has a single root reducer function. This root reducer is normally generated by calling `combineReducers()` or a similar function when the application is initialized. In order to dynamically add more reducers, we need to call that function again to re-generate the root reducer. Below, we discuss some approaches to solving this problem and reference two libraries that provide this functionality. |
5 | 12 |
|
6 | 13 | ## Basic Principle
|
7 |
| -To dynamically add Reducers to a Redux store, there are two approaches that can be taken |
8 | 14 |
|
9 | 15 | ### Using `replaceReducer`
|
10 |
| -The Redux store exposes the `replaceReducer` function, which replaces the current active reducer function with a new reducer function. We can leverage this function to add a reducer as follows: |
| 16 | + |
| 17 | +The Redux store exposes a `replaceReducer` function, which replaces the current active root reducer function with a new root reducer function. Calling it will swap the internal reducer function reference, and dispatch an action to help any newly-added slice reducers initialize themselves: |
| 18 | + |
| 19 | +```js |
| 20 | +const newRootReducer = combineReducers({ |
| 21 | + existingSlice : existingSliceReducer, |
| 22 | + newSlice : newSliceReducer, |
| 23 | +}) |
| 24 | + |
| 25 | +store.replaceReducer(newRootReducer) |
| 26 | +``` |
| 27 | + |
| 28 | +## Reducer Injection Approaches |
| 29 | + |
| 30 | +### Defining an `injectReducer` function |
| 31 | + |
| 32 | +We will likely want to call `store.replaceReducer()` from anywhere in the application. Because of that, it's helpful |
| 33 | +to define a reusable `injectReducer()` function that keeps references to all of the existing slice reducers, and attach |
| 34 | +that to the store instance. |
11 | 35 |
|
12 | 36 | ```javascript
|
13 | 37 | import { createStore } from 'redux';
|
@@ -131,8 +155,11 @@ To add a new reducer, one can now call `store.reducerManager.add("asyncState", a
|
131 | 155 | To remove a reducer, one can now call `store.reducerManager.remove("asyncState")`
|
132 | 156 |
|
133 | 157 | ## Libraries and Frameworks
|
| 158 | + |
134 | 159 | There are a few good libraries out there that can help you add the above functionality automatically:
|
135 |
| - * [`redux-dynamic-reducer`](https://github.com/ioof-holdings/redux-dynamic-reducer) |
136 |
| - * This library exposes the `addReducer` function on the Redux store to accomplish the behavior we described above. It also has React bindings which make it easier to add reducers within the React component lifecycle. |
137 |
| -* [`redux-dynamic-modules`](https://github.com/Microsoft/redux-dynamic-modules) |
138 |
| - * This library introduces the concept of a 'Redux Module', which is a bundle of Redux artifacts (reducers, middleware) that should be dynamically loaded. It also exposes a React higher-order component to load 'modules' when areas of the application come online. Additionally, it has integrations with libraries like `redux-thunk` and `redux-saga` which also help dynamically load their artifacts (thunks, sagas). |
| 160 | + |
| 161 | + * [`redux-dynostore`](https://github.com/ioof-holdings/redux-dynostore): |
| 162 | + Provides tools for building dynamic Redux stores, including dynamically adding reducers and sagas, and React bindings to help you add in association with components. |
| 163 | +* [`redux-dynamic-modules`](https://github.com/Microsoft/redux-dynamic-modules): |
| 164 | + This library introduces the concept of a 'Redux Module', which is a bundle of Redux artifacts (reducers, middleware) that should be dynamically loaded. It also exposes a React higher-order component to load 'modules' when areas of the application come online. Additionally, it has integrations with libraries like `redux-thunk` and `redux-saga` which also help dynamically load their artifacts (thunks, sagas). |
| 165 | +* [Redux Ecosystem Links: Reducers - Dynamic Reducer Injection](https://github.com/markerikson/redux-ecosystem-links/blob/master/reducers.md#dynamic-reducer-injection) |
0 commit comments