Skip to content

Commit

Permalink
Move when the callback of setState/forceUpdate is called
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Mar 17, 2017
1 parent 6c7e70c commit b78a41d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/vdom/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ export function renderComponent(component, opts, mountAll, isChild) {
if (options.afterUpdate) options.afterUpdate(component);
}

let cb = component._renderCallbacks, fn;
if (cb) while ( (fn = cb.pop()) ) fn.call(component);

if (!diffLevel && !isChild) flushMounts();
}

Expand Down
4 changes: 4 additions & 0 deletions src/vdom/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export function flushMounts() {
while ((c=mounts.pop())) {
if (options.afterMount) options.afterMount(c);
if (c.componentDidMount) c.componentDidMount();

// Ensure the nodes call cDM lifecycle method before callbacks from setState/forceUpdate
let cb = c._renderCallbacks, fn;
if (cb) while ( (fn = cb.pop()) ) fn.call(c);
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/browser/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,37 @@ describe('Lifecycle methods', () => {
expect(proto.componentDidUnmount).to.have.been.called;
});
});

// Test for Issue #556
it('should fire setState callback after componentDidMount', () => {
let log = [];

class A extends LifecycleTestComponent {
componentWillMount() {
this.setState({
a: 'a'
}, () => {
log.push('setState');
});
}
render() {
return <B />;
}
}

class B extends LifecycleTestComponent {
render() {
return <div>B</div>;
}
componentDidMount() {
log.push('didMount');
}
}

render(<A />, scratch);

expect(log).to.deep.equal(['didMount', 'setState']);
});
});

describe('Lifecycle DOM Timing', () => {
Expand Down

0 comments on commit b78a41d

Please sign in to comment.