S
SolidJS•11mo ago
AutumnCicada

If I'm using createResource, why would I use Suspense?

For example if I have
function Loader() {
const [resource] = createResource(fetchResourceFn);
return <Suspense fallback={<div>Loading....</div>}>
<ResourceRenderer resource={resource()} />
</Suspense>;
}
function Loader() {
const [resource] = createResource(fetchResourceFn);
return <Suspense fallback={<div>Loading....</div>}>
<ResourceRenderer resource={resource()} />
</Suspense>;
}
This doesn't pass typescript type checking because you still need to check if resource() is undefined or not, even though Suspense shouldn't render the ResourceRenderer until createResource is finished with its async request?
4 Replies
AutumnCicada
AutumnCicada•11mo ago
So why would you ever use Suspense over Switch + Match?
lxsmnsyc
lxsmnsyc•11mo ago
Basically createResource and Suspense is like a pair. Suspense displays a fallback when createResource is accessed, and in SSR, createResource can be awaited or streamed if read inside Suspense. However, Suspense doesn't guarantee that the value has been resolved before the UI gets to access the value, which leads to the known "issue" of accessing undefined values, which is why you use Switch/Show to guarantee that the value exists first before you access it. The reason this happens is because of Solid's concurrent rendering. Unlike the original implementation (React), Solid is non-blocking, so evaluation of the component continues even if it suspends. Now if we talk about patterns, Suspense + ErrorBoundary allows you to build scalable fallback UI, which is what conditional rendering cannot do, and is a bit more tedious to do.
AutumnCicada
AutumnCicada•11mo ago
🤦 of course the answer to my exact question is in the docs thanks for the help all Seems like the nicest pattern for this is
function Loader() {
const [resource] = createResource(fetchResourceFn);
return <Suspense fallback={<div>Loading...</div>}>
<Show when={resource()} keyed>
{(resourceVal) => <ResourceRenderer resource={resourceVal}/>}
</Show>
</Suspense>
}
function Loader() {
const [resource] = createResource(fetchResourceFn);
return <Suspense fallback={<div>Loading...</div>}>
<Show when={resource()} keyed>
{(resourceVal) => <ResourceRenderer resource={resourceVal}/>}
</Show>
</Suspense>
}
Want results from more Discord servers?
Add your server
More Posts