Skip to content

Commit

Permalink
Initialize children if they're null in suspense (#2570)
Browse files Browse the repository at this point in the history
* Initialize children if they're null in suspense

* Skip re-building children of Suspense when we got a fresh vnode

Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>
  • Loading branch information
sventschui and JoviDeCroock authored Jul 13, 2020
1 parent 4937116 commit aea0726
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compat/src/suspense.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ Suspense.prototype._childDidSuspend = function(promise, suspendingComponent) {

Suspense.prototype.render = function(props, state) {
if (this._detachOnNextRender) {
this._vnode._children[0] = detachedClone(this._detachOnNextRender);
// When the Suspense's _vnode was created by a call to createVNode
// (i.e. due to a setState further up in the tree)
// it's _children prop is null, in this case we "forget" about the parked vnodes to detach
if (this._vnode._children)
this._vnode._children[0] = detachedClone(this._detachOnNextRender);
this._detachOnNextRender = null;
}

Expand Down
48 changes: 48 additions & 0 deletions compat/test/browser/suspense.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,54 @@ describe('suspense', () => {
});
});

it('should support a call to setState before rendering the fallback', () => {
const LazyComp = ({ name }) => <div>Hello from {name}</div>;

/** @type {() => Promise<void>} */
let resolve;
const Lazy = lazy(() => {
const p = new Promise(res => {
resolve = () => {
res({ default: LazyComp });
return p;
};
});

return p;
});

/** @type {(Object) => void} */
let setState;
class App extends Component {
constructor(props) {
super(props);
this.state = {};
setState = this.setState.bind(this);
}
render(props, state) {
return (
<Fragment>
<Suspense fallback={<div>Suspended...</div>}>
<Lazy name="LazyComp" />
</Suspense>
</Fragment>
);
}
}

render(<App />, scratch); // Render initial state

setState({ foo: 'bar' });
rerender();

expect(scratch.innerHTML).to.eql(`<div>Suspended...</div>`);

return resolve().then(() => {
rerender();
expect(scratch.innerHTML).to.eql(`<div>Hello from LazyComp</div>`);
});
});

it('lazy should forward refs', () => {
const LazyComp = () => <div>Hello from LazyComp</div>;
let ref = {};
Expand Down

0 comments on commit aea0726

Please sign in to comment.