Skip to content

Commit 7d1cabd

Browse files
authored
Merge pull request #2528 from sbakkila/master
Add design decisions to FAQ document
2 parents 69db792 + 0b496c2 commit 7d1cabd

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

docs/FAQ.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343
- [Do I have to deep-clone my state in a reducer? Isn't copying my state going to be slow?](/docs/faq/Performance.md#performance-clone-state)
4444
- [How can I reduce the number of store update events?](/docs/faq/Performance.md#performance-update-events)
4545
- [Will having “one state tree” cause memory problems? Will dispatching many actions take up memory?](/docs/faq/Performance.md#performance-state-memory)
46+
- **Design Decisions**
47+
- [Why doesn't Redux pass the state and action to subscribers?](/docs/faq/DesignDecisions.md#does-not-pass-state-action-to-subscribers)
48+
- [Why doesn't Redux support using classes for actions and reducers?](/docs/faq/DesignDecisions.md#does-not-support-classes)
49+
- [Why does the middleware signature use currying?](/docs/faq/DesignDecisions.md#why-currying)
50+
- [Why does applyMiddleware use a closure for dispatch?](/docs/faq/DesignDecisions.md#closure-dispatch)
51+
- [Why doesn't `combineReducers` include a third argument with the entire state when it calls each reducer?](/docs/faq/DesignDecisions.md#combineReducers-limitations)
52+
- [Why doesn't `mapDispatchToProps` allow use of return values from `getState()` or `mapStateToProps()`?](/docs/faq/DesignDecisions.md#no-asynch-in-mapDispatchToProps)
4653
- **React Redux**
4754
- [Why isn't my component re-rendering, or my mapStateToProps running?](/docs/faq/ReactRedux.md#react-not-rerendering)
4855
- [Why is my component re-rendering too often?](/docs/faq/ReactRedux.md#react-rendering-too-often)

docs/faq/DesignDecisions.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Redux FAQ: Design Decisions
2+
3+
## Table of Contents
4+
5+
- [Why doesn't Redux pass the state and action to subscribers?](#does-not-pass-state-action-to-subscribers)
6+
- [Why doesn't Redux support using classes for actions and reducers?](#does-not-support-classes)
7+
- [Why does the middleware signature use currying?](#why-currying)
8+
- [Why does applyMiddleware use a closure for dispatch?](#closure-dispatch)
9+
- [Why doesn't `combineReducers` include a third argument with the entire state when it calls each reducer?](#combineReducers-limitations)
10+
- [Why doesn't mapDispatchToProps allow use of return values from `getState()` or `mapStateToProps()`?](#no-asynch-in-mapDispatchToProps)
11+
12+
13+
## Design Decisions
14+
15+
<a id="does-not-pass-state-action-to-subscribers"></a>
16+
### Why doesn't Redux pass the state and action to subscribers?
17+
Subscribers are intended to respond to the state value itself, not the action. Updates to the state processed synchronously, but notifications to subscribers are batched or debounced, meaning that subscribers are not always notified with every action. This is a common [performance optimization](http://redux.js.org/docs/faq/Performance.html#performance-update-events) to avoid repeated re-rendering.
18+
19+
Batching or debouncing is possible by using enhancers to override `store.dispatch` to change the way that subscribers are notified. Also, there are libraries that change Redux to process actions in batches to optimize performance and avoid repeated re-rendering:
20+
* [redux-batch](https://github.com/manaflair/redux-batch) allows passing an array of actions to `store.dispatch()` with only one notification,
21+
* [redux-batched-subscribe](https://github.com/tappleby/redux-batched-subscribe) allows batching of subscribe notifications that occur as a result of dispatches.
22+
23+
The intended guarantee is that Redux eventually calls all subscribers with the most recent state, but not that it always calls each subscriber for each action. The store state is available in the subscriber simply by calling `store.getState()`. The action cannot be made available in the subscribers without breaking the way that actions are batched.
24+
25+
A potential use-case for using the action inside a subscriber -- which is an unsupported feature -- is to ensure that a component only re-renders after certain kinds of actions. Re-rendering should instead be controlled instead through:
26+
1. the [shouldComponentUpdate](https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate) lifecycle method
27+
2. the [virtual DOM equality check (vDOMEq)](https://facebook.github.io/react/docs/optimizing-performance.html#avoid-reconciliation)
28+
3. [React.PureComponent](https://facebook.github.io/react/docs/optimizing-performance.html#examples)
29+
4. Using React-Redux: use [mapStateToProps](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) to subscribe components to only the parts of the store that they need.
30+
31+
#### Further Information
32+
**Articles**
33+
* [How can I reduce the number of store update events?](./Performance.md#performance-update-events)
34+
35+
**Discussions**
36+
* [#580: Why doesn't Redux pass the state to subscribers?](https://github.com/reactjs/redux/issues/580)
37+
* [#2214: Alternate Proof of Concept: Enhancer Overhaul -- more on debouncing](https://github.com/reactjs/redux/pull/2214)
38+
39+
<a id="does-not-support-classes"></a>
40+
### Why doesn't Redux support using classes for actions and reducers?
41+
The pattern of using functions, called action creators, to return action objects may seem counterintuitive to programmers with a lot of Object Oriented Programming experience, who would see this is a strong use-case for Classes and instances. Class instances for action objects and reducers are not supported because class instances make serialization and deserialization tricky. Deserialization methods like `JSON.parse(string)` will return a plain old Javascript object rather than class instances.
42+
43+
As described in the [Store FAQ](./OrganizingState.md#organizing-state-non-serializable), if you are okay with things like persistence and time-travel debugging not working as intended, you are welcome to put non-serializable items into your Redux store.
44+
45+
Serialization enables the brower to store all actions that have been dispatched, as well as the previous store states, with much less memory. Rewinding and 'hot reloading' the store is central to the Redux developer experience and the function of Redux DevTools. This also enables deserialized actions to be stored on the server and re-serialized in the brower in the case of server-side rendering with Redux.
46+
47+
#### Further Information
48+
**Articles**
49+
* [Can I put functions, promises, or other non-serializable items in my store state?](./OrganizingState.md#organizing-state-non-serializable)
50+
51+
**Discussions**
52+
* [#1171: Why doesn't Redux use classes for actions and reducers?](https://github.com/reactjs/redux/issues/1171#issuecomment-196819727)
53+
54+
<a id="why-currying"></a>
55+
### Why does the middleware signature use currying?
56+
The [curried function signature](https://github.com/reactjs/redux/issues/1744) of declaring middleware is [deemed unnecessary](https://github.com/reactjs/redux/pull/784) by some, because both store and next are available when the applyMiddleware function is executed. This issue has been determined to not be [worth introducing breaking changes](https://github.com/reactjs/redux/issues/1744).
57+
58+
#### Further Information
59+
**Discussions**
60+
* Why does the middleware signature use currying?
61+
* See - [#55](https://github.com/reactjs/redux/pull/55), [#534](https://github.com/reactjs/redux/issues/534), [#784](https://github.com/reactjs/redux/pull/784), [#922](https://github.com/reactjs/redux/issues/922), [#1744](https://github.com/reactjs/redux/issues/1744)
62+
63+
<a id="closure-dispatch"></a>
64+
### Why does `applyMiddleware` use a closure for `dispatch`?
65+
`applyMiddleware` takes the existing dispatch from the store and closes over it to create the initial chain of middlewares that have been invoked with an object that exposes the getState and dispatch functions, which enables middlewares that [rely on dispatch during initialization](https://github.com/reactjs/redux/pull/1592) to run.
66+
67+
#### Further Information
68+
**Discussions**
69+
* Why does applyMiddleware use a closure for dispatch?
70+
* See - [#1592](https://github.com/reactjs/redux/pull/1592) and [#2097](https://github.com/reactjs/redux/issues/2097)
71+
72+
<a id="combineReducers-limitations"></a>
73+
### Why doesn't `combineReducers` include a third argument with the entire state when it calls each reducer?
74+
75+
`combineReducers` is opinionated to encourage splitting reducer logic by domain. As stated in [Beyond `combineReducers`](../recipes/reducers/BeyondCombineReducers.md),`combineReducers` is deliberately limited to handle a single common use case: updating a state tree that is a plain Javascript object by delegating the work of updating each slice of state to a specific slice reducer.
76+
77+
It's not immediately obvious what a potential third argument to each reducer should be: the entire state tree, some callback function, some other part of the state tree, etc. If `combineReducers` doesn't fit your use case, consider using libraries like [combineSectionReducers](https://github.com/ryo33/combine-section-reducers) or [reduceReducers](https://github.com/acdlite/reduce-reducers) for other options with deeply nested reducers and reducers that require access to the global state.
78+
79+
If none of the published utilities solve your use case, you can always write a function yourself that does just exactly what you need.
80+
81+
#### Further information
82+
**Articles**
83+
* [Beyond `combineReducers`](../recipes/reducers/BeyondCombineReducers.md)
84+
85+
**Discussions**
86+
* [#1768 Allow reducers to consult global state](https://github.com/reactjs/redux/pull/1768)
87+
88+
<a id="no-asynch-in-mapDispatchToProps"></a>
89+
### Why doesn't `mapDispatchToProps` allow use of return values from `getState()` or `mapStateToProps()`?
90+
91+
There have been requests to use either the entire `state` or the return value of `mapState` inside of `mapDispatch`, so that when functions are declared inside of `mapDispatch`, they can close over the latest returned values from the store.
92+
93+
This approach is not supported in `mapDispatch` because it would mean also calling `mapDispatch` every time the store is updated. This would cause the re-creation of functions with every state update, thus adding a lot of performance overhead.
94+
95+
The preferred way to handle this use-case--needing to alter props based on the current state and mapDispatchToProps functions--is to work from mergeProps, the third argument to the connect function. If specified, it is passed the result of `mapStateToProps()`, `mapDispatchToProps()`, and the container component's props. The plain object returned from `mergeProps` will be passed as props to the wrapped component.
96+
97+
#### Further information
98+
**Discussions**
99+
* [#237 Why doesn't mapDispatchToProps allow use of return values from getState() or mapStateToProps()?](https://github.com/reactjs/react-redux/issues/237)

0 commit comments

Comments
 (0)