Skip to content

Commit

Permalink
Merge pull request #179 from developit/fix_did_update_prop
Browse files Browse the repository at this point in the history
Fix componentDidUpdate called even if shouldComponentUpdate returned false
  • Loading branch information
marvinhagemeister authored Jan 9, 2019
2 parents b8945b0 + 4031c37 commit 3bb17d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export function diff(dom, parentDom, newVNode, oldVNode, context, isSvg, append,
if (c!=null) {
while (p=c._renderCallbacks.pop()) p();

if (!isNew && c.componentDidUpdate!=null) {
// Don't call componentDidUpdate on mount or when we bailed out via
// `shouldComponentUpdate`
if (!isNew && oldProps!=null && c.componentDidUpdate!=null) {
c.componentDidUpdate(oldProps, oldState, oldContext);
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/browser/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,32 @@ describe('Lifecycle methods', () => {
expect(curState).to.deep.equal({ value: 4 });
});

it('cDU should not be called when sDU returned false', () => {
let spy = sinon.spy();
let c;

class App extends Component {
constructor() {
super();
c = this;
}

shouldComponentUpdate() {
return false;
}

componentDidUpdate(prevProps) {
spy(prevProps);
}
}

render(<App />, scratch);
c.setState({});
rerender();

expect(spy).to.not.be.called;
});

it('prevState argument should be the same object if state doesn\'t change', () => {
let changeProps, cduPrevState, cduCurrentState;

Expand Down

0 comments on commit 3bb17d2

Please sign in to comment.