SolidJSS
SolidJSโ€ข12mo agoโ€ข
8 replies
JOE

How to use server function & suspense

I will be frank, I have some experience with React but this is my first dive into Solid coming from Astro
I'm trying to have a fallback loading component, that is replaced by data fetched from the server when available
I figured the best way to do this was an asynchronous server function, I am not claiming there is a bug I think I just did something wrong
Here is my high-level code since I can't really share more:
async function getUserInfo() {
  "use server";
  // Mimics server speed, network latency, etc.
  await new Promise((resolve) => setTimeout(resolve, 300));

  // TO-DO: real data / connections here
  return {
    avatar: "~/assets/img/placeholders/generic-avatar.jpg",
    name: "User Name",
    email: "useremail@email.com",
  };
}

//... component defined
  const [fetchedInfo] = createResource(getUserInfo);

  return (
    <Suspense fallback={<UserInfoFallback />}>
          <img
            class="aspect-square size-full"
            src={fetchedInfo()?.avatar}
            decoding="sync"
            loading="eager"
            alt="User Avatar"
            draggable="false"
          />
        <span class="text-neutral-content flex w-full flex-col gap-0.5 truncate text-left">
          <p class="block truncate text-sm font-semibold">
            {fetchedInfo()?.name}
          </p>
          <p class="block truncate text-[0.7rem] font-light">
            {fetchedInfo()?.email}
          </p>
        </span>
    </Suspense> )
Was this page helpful?