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

Initialize children if they're null in suspense #2570

Merged
merged 4 commits into from
Jul 13, 2020
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
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(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want you could use the createLazy helper to simplify this to const [Lazy, resolve] = createLazy() and then pass LazyComp into resolve below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh okay, just copy&pasted from another test. I'll check

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