Skip to content

Commit

Permalink
this.props should be old props in Presenter::update
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Oct 31, 2016
1 parent 6066c27 commit 7caa617
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 10.3.5

- `this.props` within `Presenter::update` should be the old props.

## 10.3.4

- Fixed internal loop iteration bug where change emissions of a
Expand Down
28 changes: 21 additions & 7 deletions src/addons/presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ class Presenter extends React.Component {
this.render = wrappedRender
}

_setRepo (repo) {
this.repo = repo
this.setup(repo, this.props)
}

_isImpure () {
if (this.props.hasOwnProperty('pure')) {
return this.props.pure !== true
}

return this.repo.pure !== true
}

/**
* Called when a presenter is created, before it has calculated a view model.
* Useful for fetching data and other prep-work.
Expand All @@ -51,6 +64,12 @@ class Presenter extends React.Component {
// NOOP
}

componentWillReceiveProps (next) {
if (this._isImpure() || !shallowEqual(next, this.props)) {
this.update(this.repo, next)
}
}

/**
* Opposite of setup. Useful for cleaning up side-effects.
*
Expand Down Expand Up @@ -140,7 +159,7 @@ class PresenterContext extends React.Component {
}

componentWillMount () {
this.props.presenter.setup(this.repo, this.safeProps(this.props))
this.props.presenter._setRepo(this.repo)

this.updatePropMap(this.props)
this.updateState()
Expand All @@ -158,7 +177,6 @@ class PresenterContext extends React.Component {
componentWillReceiveProps (next) {
if (this.isImpure() || !shallowEqual(next, this.props)) {
this.updatePropMap(next)
this.props.presenter.update(this.repo, this.safeProps(next))
}

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

isImpure () {
if (this.props.hasOwnProperty('pure')) {
return this.props.pure !== true
}

return this.repo.pure !== true
return this.props.presenter._isImpure()
}

safeProps (props) {
Expand Down
18 changes: 18 additions & 0 deletions test/addons/presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,21 @@ test('setup is called before the initial model', function () {

expect(sequence).toEqual(['setup', 'model'])
})

describe('update', function() {

test('it has access to the old props when update is called', function () {
const callback = jest.fn()

class Test extends Presenter {
update (repo, { color }) {
callback(this.props.color, color)
}
}

mount(<Test color="red"><p>Hey</p></Test>).setProps({ color: 'blue' })

expect(callback).toHaveBeenCalledWith('red', 'blue')
})

})

0 comments on commit 7caa617

Please sign in to comment.