Skip to content

Commit 2338655

Browse files
committed
Rework code splitting page per review feedback and add page
1 parent 95ff8d5 commit 2338655

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

docs/recipes/CodeSplitting.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1+
---
2+
id: code-splitting
3+
title: Code Splitting
4+
sidebar_label: Code Splitting
5+
hide_title: true
6+
---
7+
18
# Code Splitting
29
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.
310

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.
512

613
## Basic Principle
7-
To dynamically add Reducers to a Redux store, there are two approaches that can be taken
814

915
### 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.
1135

1236
```javascript
1337
import { createStore } from 'redux';
@@ -131,8 +155,11 @@ To add a new reducer, one can now call `store.reducerManager.add("asyncState", a
131155
To remove a reducer, one can now call `store.reducerManager.remove("asyncState")`
132156

133157
## Libraries and Frameworks
158+
134159
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)

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"recipes/implementing-undo-history",
4242
"recipes/isolating-redux-sub-apps",
4343
"recipes/using-immutablejs-with-redux",
44+
"recipes/code-splitting",
4445
{
4546
"type": "subcategory",
4647
"label": "Structuring Reducers",

0 commit comments

Comments
 (0)