Skip to content

Commit 5a72594

Browse files
committed
Fix formatting
1 parent 2338655 commit 5a72594

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

docs/recipes/CodeSplitting.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ hide_title: true
66
---
77

88
# Code Splitting
9+
910
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.
1011

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.
12+
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.
1213

1314
## Basic Principle
1415

1516
### Using `replaceReducer`
1617

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+
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:
1819

1920
```js
2021
const newRootReducer = combineReducers({
21-
existingSlice : existingSliceReducer,
22-
newSlice : newSliceReducer,
22+
existingSlice: existingSliceReducer,
23+
newSlice: newSliceReducer
2324
})
2425

2526
store.replaceReducer(newRootReducer)
@@ -29,48 +30,49 @@ store.replaceReducer(newRootReducer)
2930

3031
### Defining an `injectReducer` function
3132

32-
We will likely want to call `store.replaceReducer()` from anywhere in the application. Because of that, it's helpful
33+
We will likely want to call `store.replaceReducer()` from anywhere in the application. Because of that, it's helpful
3334
to define a reusable `injectReducer()` function that keeps references to all of the existing slice reducers, and attach
34-
that to the store instance.
35+
that to the store instance.
3536

3637
```javascript
37-
import { createStore } from 'redux';
38+
import { createStore } from 'redux'
3839

3940
// Define the Reducers that will always be present in the appication
4041
const staticReducers = {
41-
users: usersReducer,
42-
posts: postsReducer
43-
};
42+
users: usersReducer,
43+
posts: postsReducer
44+
}
4445

4546
// Configure the store
4647
export default function configureStore(initialState) {
47-
const store = createStore(createReducer(), initialState);
48+
const store = createStore(createReducer(), initialState)
4849

4950
// Add a dictionary to keep track of the registered async reducers
50-
store.asyncReducers = {};
51+
store.asyncReducers = {}
5152

5253
// Create an inject reducer function
5354
// This function adds the async reducer, and creates a new combined reducer
5455
store.injectReducer = (key, asyncReducer) => {
55-
this.asyncReducers[key] = asyncReducer;
56-
this.replaceReducer(createReducer(this.asyncReducers));
57-
};
56+
this.asyncReducers[key] = asyncReducer
57+
this.replaceReducer(createReducer(this.asyncReducers))
58+
}
5859

5960
// Return the modified store
60-
return store;
61+
return store
6162
}
6263

6364
function createReducer(asyncReducers) {
64-
return combineReducers({
65-
...staticReducers,
66-
...asyncReducers
67-
});
65+
return combineReducers({
66+
...staticReducers,
67+
...asyncReducers
68+
})
6869
}
69-
7070
```
71+
7172
Now, one just needs to call `store.injectReducer` to add a new reducer to the store.
7273

7374
### Using a 'Reducer Manager'
75+
7476
Another approach is to create a 'Reducer Manager' object, which keeps track of all the registered reducers and exposes a `reduce()` function. Consider the following example:
7577

7678
```javascript
@@ -158,8 +160,8 @@ To remove a reducer, one can now call `store.reducerManager.remove("asyncState")
158160

159161
There are a few good libraries out there that can help you add the above functionality automatically:
160162

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)
163+
- [`redux-dynostore`](https://github.com/ioof-holdings/redux-dynostore):
164+
Provides tools for building dynamic Redux stores, including dynamically adding reducers and sagas, and React bindings to help you add in association with components.
165+
- [`redux-dynamic-modules`](https://github.com/Microsoft/redux-dynamic-modules):
166+
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).
167+
- [Redux Ecosystem Links: Reducers - Dynamic Reducer Injection](https://github.com/markerikson/redux-ecosystem-links/blob/master/reducers.md#dynamic-reducer-injection)

docs/recipes/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ hide_title: true
99

1010
These are some use cases and code snippets to get you started with Redux in a real app. They assume you understand the topics in [basic](../basics/README.md) and [advanced](../advanced/README.md) tutorials.
1111

12-
1312
- [Configuring Your Store](ConfiguringYourStore.md)
1413
- [Migrating to Redux](MigratingToRedux.md)
1514
- [Using Object Spread Operator](UsingObjectSpreadOperator.md)

0 commit comments

Comments
 (0)