Skip to content

Commit 7a88aea

Browse files
committed
added pure option
- only re-render component if props or requests shallowly changed - this.setAtomicState now creates new objects for all 4 keys - added tests for `pure: true` and `pure: false` - added `pure` option to api docs
1 parent 49b221b commit 7a88aea

File tree

3 files changed

+497
-39
lines changed

3 files changed

+497
-39
lines changed

docs/api.md

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const b = a.defaults({ withRef: false })
122122

123123
* `[newOptions = {}]` *(Object)*: An object with any of the following keys:
124124
- `withRef` *(Boolean)*: If `true`, the connector will store a ref to the wrapped component instance and make it available via the `getWrappedInstance()` method. Defaults to `false`.
125+
- `pure` *(Boolean)*: If `true`, the connector will treat the wrapped component and the mapPropsToRequestsToProps function as pure, recomputing requests and re-rendering only when props are shallowly different. If `false`, recompute and re-render every time props change. Defaults to `true`.
125126

126127
### `PromiseState`
127128

src/components/connect.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ function connect(mapPropsToRequestsToProps, defaults, options) {
9696
checkTypes(defaults)
9797

9898
options = Object.assign({
99-
withRef: false
99+
withRef: false,
100+
pure: true
100101
}, options)
101102

102103
// Helps track hot reloading.
@@ -183,13 +184,19 @@ function connect(mapPropsToRequestsToProps, defaults, options) {
183184

184185
componentWillReceiveProps(nextProps, nextContext) {
185186
if (
187+
!options.pure ||
186188
(dependsOnProps && !shallowEqual(omitChildren(this.props), omitChildren(nextProps))) ||
187189
(dependsOnContext && !shallowEqual(this.context, nextContext))
188190
) {
189191
this.refetchDataFromProps(nextProps, nextContext)
190192
}
191193
}
192194

195+
shouldComponentUpdate(nextProps, nextState) {
196+
return !options.pure ||
197+
this.state.data != nextState.data || !shallowEqual(this.props, nextProps)
198+
}
199+
193200
componentWillUnmount() {
194201
this.clearAllRefreshTimeouts()
195202
this._unmounted = true
@@ -325,18 +332,22 @@ function connect(mapPropsToRequestsToProps, defaults, options) {
325332

326333
return {
327334
startedAts: Object.assign(
335+
{},
328336
prevState.startedAts, {
329337
[prop]: startedAt
330338
}),
331339
mappings: Object.assign(
340+
{},
332341
prevState.mappings, {
333342
[prop]: mapping
334343
}),
335344
data: Object.assign(
345+
{},
336346
prevState.data, {
337347
[prop]: datum
338348
}),
339349
refreshTimeouts: Object.assign(
350+
{},
340351
prevState.refreshTimeouts, {
341352
[prop]: refreshTimeout
342353
})

0 commit comments

Comments
 (0)