SolidJSS
SolidJSโ€ข3y agoโ€ข
3 replies
bigmistqke

`Suspense` deep dive ๐Ÿฌ

I am dealing with some inconsistencies in how Suspense works between react vs solid while working on #solid-three. I wanna check up if my understanding of their mechanics is correct:

react

- In react, a Suspense is triggered by throwing a promise. This needs to be done in either the component-body or the jsx, no effects/memos. All code will run until that point. The code throws before anything can mount, nothing runs after the throw: no useEffect and useLayoutEffect, child-components do not run. The suspense-boundary catches the promise and re-renders its children once the promise is resolved.
- โœ”๏ธ <Suspense><div>{resource()}</div></Suspense>
- โœ”๏ธ <Suspense>{() => { resource(); return <></> }</Suspense>
- โœ”๏ธ <Suspense><Component prop={resource()}></Suspense>
- โŒ <Suspense>{() => { useLayoutEffect(() => resource()); return <></> }</Suspense> will throw instead

solid

- In solid, a Suspense is a 'special' signal. To trigger a Suspense boundary, the signal needs to be added directly in the jsx-tree or used inside a createRenderEffect or a createMemo. Unlike react, using it directly inside the component body will not trigger Suspense. Nothing throws, so code below the resource will still run. All siblings of the suspense will do their initial run.
createEffect
and onMount will only run once the resource is resolved, createRenderEffect and createMemo do run during initial run.
- โœ”๏ธ <Suspense><div>{resource() || resource}</div></Suspense>
- โŒ <Suspense>{() => { resource(); return <></> }</Suspense>
- โŒ <Suspense><Component prop={resource() || resource}></Suspense>:(unless Component adds it to the jsx-tree or uses it inside createRenderEffect / createMemo)
- โœ”๏ธ <Suspense>{() => { createRenderEffect(() => resource()); return <></> }</Suspense>
Was this page helpful?