Skip to content

Commit

Permalink
Add support for null fallbacks in Suspense (+3 B) #2030
Browse files Browse the repository at this point in the history
Merge pull request #2030 from preactjs/fix/suspense-null-fallback
  • Loading branch information
andrewiggins authored Oct 23, 2019
2 parents 2a3f92d + 180342d commit 90e0db0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
6 changes: 5 additions & 1 deletion compat/src/suspense.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ Suspense.prototype._childDidSuspend = function(promise) {
c._suspensions.pop();

if (c._suspensions.length == 0) {
unmount(c.props.fallback);
// If fallback is null, don't try to unmount it
// `unmount` expects a real VNode, not null values
if (c.props.fallback) {
unmount(c.props.fallback);
}
c._vnode._dom = null;

c._vnode._children = c.state._parkedChildren;
Expand Down
47 changes: 46 additions & 1 deletion compat/test/browser/suspense.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,32 @@ class Catcher extends Component {
}

describe('suspense', () => {
let scratch, rerender;
let scratch, rerender, unhandledEvents = [];

function onUnhandledRejection(event) {
unhandledEvents.push(event);
}

beforeEach(() => {
scratch = setupScratch();
rerender = setupRerender();

unhandledEvents = [];
if ('onunhandledrejection' in window) {
window.addEventListener('unhandledrejection', onUnhandledRejection);
}
});

afterEach(() => {
teardown(scratch);

if ('onunhandledrejection' in window) {
window.removeEventListener('unhandledrejection', onUnhandledRejection);

if (unhandledEvents.length) {
throw unhandledEvents[0].reason;
}
}
});

it('should support lazy', () => {
Expand Down Expand Up @@ -779,4 +796,32 @@ describe('suspense', () => {
);
});
});

it('should support null fallback', () => {
const [Suspender, suspend] = createSuspender(() => <div>Hello</div>);

render(
<div id="wrapper">
<Suspense fallback={null}>
<div id="inner">
<Suspender />
</div>
</Suspense>
</div>,
scratch
);
expect(scratch.innerHTML).to.equal(
`<div id="wrapper"><div id="inner"><div>Hello</div></div></div>`
);

const [resolve] = suspend();
rerender();
expect(scratch.innerHTML).to.equal(`<div id="wrapper"></div>`);

return resolve(() => <div>Hello2</div>).then(() => {
rerender();
expect(scratch.innerHTML).to.equal(`<div id="wrapper"><div id="inner"><div>Hello2</div></div></div>`);
});
});

});
2 changes: 1 addition & 1 deletion mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
"$_unmount": "_e"
}
}
}
}

0 comments on commit 90e0db0

Please sign in to comment.