Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) - nested setState calls with accurate this.state #1748

Merged
merged 5 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi

c.base = newVNode._dom;

while (tmp=c._renderCallbacks.pop()) tmp.call(c);
while (tmp=c._renderCallbacks.pop()) {
if (c._nextState) { c.state = c._nextState; }
marvinhagemeister marked this conversation as resolved.
Show resolved Hide resolved
tmp.call(c);
}

// Don't call componentDidUpdate on mount or when we bailed out via
// `shouldComponentUpdate`
Expand Down
36 changes: 36 additions & 0 deletions test/browser/components.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ describe('Components', () => {
expect(spy).to.not.be.called;
});

it('should accurately call nested setState callbacks', () => {
let states = [];
let finalState;
class Foo extends Component {
constructor(props) {
super(props);
this.state = { a: 'b' };
}

componentDidMount() {
states.push(this.state);
// eslint-disable-next-line
marvinhagemeister marked this conversation as resolved.
Show resolved Hide resolved
this.setState({ a: 'a' }, () => {
states.push(this.state);
this.setState({ a: 'c' }, () => {
states.push(this.state);
});
});
}

render() {
finalState = this.state;
return <p>Test</p>;
}
}

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

let [firstState, secondState, thirdState] = states;
expect(finalState).to.deep.equal({ a: 'c' });
expect(firstState).to.deep.equal({ a: 'b' });
expect(secondState).to.deep.equal({ a: 'a' });
expect(thirdState).to.deep.equal({ a: 'c' });
});

it('should initialize props & context but not state in Component constructor', () => {
// Not initializing state matches React behavior: https://codesandbox.io/s/rml19v8o2q
class Foo extends Component {
Expand Down