Skip to content

Commit 5e7acf3

Browse files
Fix state not being flushed after cWM
1 parent d22211e commit 5e7acf3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
112112
c.context = cctx;
113113
if (nodeName.getDerivedStateFromProps) c.state = assign(assign({}, c.state), nodeName.getDerivedStateFromProps(c.props, c.state));
114114
else if (c.componentWillMount) c.componentWillMount();
115+
116+
// If the user called setState in cWM we need to flush pending,
117+
// state updates. This is the same behaviour in React.
118+
c.state = c._nextState !== c.state
119+
? c._nextState : c.__s!==c.state
120+
? c.__s : c.state;
121+
115122
rendered = c.render(c.props, c.state, c.context);
116123
}
117124

test/render.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ describe('render', () => {
363363
componentWillMount() {
364364
this.setState({ updated: true });
365365
}
366-
367366
render() {
368367
return (
369368
<p>
@@ -373,7 +372,7 @@ describe('render', () => {
373372
}
374373
}
375374

376-
expect(render(<Test />)).to.equal('<p>false</p>');
375+
expect(render(<Test />)).to.equal('<p>true</p>');
377376
});
378377

379378
it('should invoke getDerivedStateFromProps rather than componentWillMount', () => {

0 commit comments

Comments
 (0)