Skip to content

Commit 7caa617

Browse files
committed
this.props should be old props in Presenter::update
1 parent 6066c27 commit 7caa617

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 10.3.5
4+
5+
- `this.props` within `Presenter::update` should be the old props.
6+
37
## 10.3.4
48

59
- Fixed internal loop iteration bug where change emissions of a

src/addons/presenter.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ class Presenter extends React.Component {
2828
this.render = wrappedRender
2929
}
3030

31+
_setRepo (repo) {
32+
this.repo = repo
33+
this.setup(repo, this.props)
34+
}
35+
36+
_isImpure () {
37+
if (this.props.hasOwnProperty('pure')) {
38+
return this.props.pure !== true
39+
}
40+
41+
return this.repo.pure !== true
42+
}
43+
3144
/**
3245
* Called when a presenter is created, before it has calculated a view model.
3346
* Useful for fetching data and other prep-work.
@@ -51,6 +64,12 @@ class Presenter extends React.Component {
5164
// NOOP
5265
}
5366

67+
componentWillReceiveProps (next) {
68+
if (this._isImpure() || !shallowEqual(next, this.props)) {
69+
this.update(this.repo, next)
70+
}
71+
}
72+
5473
/**
5574
* Opposite of setup. Useful for cleaning up side-effects.
5675
*
@@ -140,7 +159,7 @@ class PresenterContext extends React.Component {
140159
}
141160

142161
componentWillMount () {
143-
this.props.presenter.setup(this.repo, this.safeProps(this.props))
162+
this.props.presenter._setRepo(this.repo)
144163

145164
this.updatePropMap(this.props)
146165
this.updateState()
@@ -158,7 +177,6 @@ class PresenterContext extends React.Component {
158177
componentWillReceiveProps (next) {
159178
if (this.isImpure() || !shallowEqual(next, this.props)) {
160179
this.updatePropMap(next)
161-
this.props.presenter.update(this.repo, this.safeProps(next))
162180
}
163181

164182
this.updateState()
@@ -177,11 +195,7 @@ class PresenterContext extends React.Component {
177195
}
178196

179197
isImpure () {
180-
if (this.props.hasOwnProperty('pure')) {
181-
return this.props.pure !== true
182-
}
183-
184-
return this.repo.pure !== true
198+
return this.props.presenter._isImpure()
185199
}
186200

187201
safeProps (props) {

test/addons/presenter.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,21 @@ test('setup is called before the initial model', function () {
510510

511511
expect(sequence).toEqual(['setup', 'model'])
512512
})
513+
514+
describe('update', function() {
515+
516+
test('it has access to the old props when update is called', function () {
517+
const callback = jest.fn()
518+
519+
class Test extends Presenter {
520+
update (repo, { color }) {
521+
callback(this.props.color, color)
522+
}
523+
}
524+
525+
mount(<Test color="red"><p>Hey</p></Test>).setProps({ color: 'blue' })
526+
527+
expect(callback).toHaveBeenCalledWith('red', 'blue')
528+
})
529+
530+
})

0 commit comments

Comments
 (0)