Skip to content

Commit

Permalink
Fix stale state if sCU returned false
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 7, 2019
1 parent 27e4e22 commit 661dbfb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function diff(dom, parentDom, newVNode, oldVNode, context, isSvg, excessD

if (!c._force && c.shouldComponentUpdate!=null && c.shouldComponentUpdate(newVNode.props, s, cctx)===false) {
c.props = newVNode.props;
c.state = s;
c._dirty = false;
break outer;
}
Expand Down
34 changes: 34 additions & 0 deletions test/browser/lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,40 @@ describe('Lifecycle methods', () => {

expect(spy).to.be.calledOnce;
});

it('should update state reference when sCU returns false', () => {
let spy = sinon.spy();

let updateState;
class Foo extends Component {
constructor() {
super();
this.state = { foo: 1 };
updateState = () => this.setState({ foo: 2 });
}

shouldComponentUpdate(_, nextState) {
if (nextState !== this.state) {
spy(this.state, nextState);
return false;
}
return true;
}
}

render(<Foo />, scratch);
updateState();
rerender();

expect(spy).to.be.calledOnce;
expect(spy).to.be.calledWithMatch({ foo: 1 }, { foo: 2 });

updateState();
rerender();

expect(spy).to.be.calledWithMatch({ foo: 2 }, { foo: 2 });
expect(spy).to.be.calledTwice;
});
});


Expand Down

0 comments on commit 661dbfb

Please sign in to comment.