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

[Bug?]: Screen flickers on createResource changed #1615

Closed
2 tasks done
brubanov opened this issue Aug 26, 2024 · 3 comments
Closed
2 tasks done

[Bug?]: Screen flickers on createResource changed #1615

brubanov opened this issue Aug 26, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@brubanov
Copy link

brubanov commented Aug 26, 2024

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Source

demo: https://stackblitz.com/edit/github-r5fquo?file=src%2Froutes%2Fabout.tsx

import { createSignal, createResource } from 'solid-js';
import { render } from 'solid-js/web';

const fetchUser = async (id) =>
  (await fetch(`https://swapi.dev/api/people/${id}/`)).json();

export default function Select() {
  const [userId, setUserId] = createSignal();
  const [user, { refetch }] = createResource(userId, fetchUser);

  return (
    <>
      <input
        type="number"
        min="1"
        placeholder="Enter Numeric Id"
        onInput={(e) => setUserId(e.currentTarget.value)}
      />
      <button onClick={refetch}>refetch</button>
      <span>{user.loading && 'Loading...'}</span>
      <div>
        <pre>{JSON.stringify(user(), null, 2)}</pre>
      </div>
    </>
  );
}

Steps to reproduce 🕹

Steps:

  1. createResource
  2. update resource
Screen.Recording.2024-08-26.at.18.44.19.mov
@brubanov brubanov added the bug Something isn't working label Aug 26, 2024
@ryansolid
Copy link
Member

More than likely this is triggering a top level Suspense. I suggest inserting lower Suspense boundaries add loading states lower in the hierarchy.

@brubanov
Copy link
Author

More than likely this is triggering a top level Suspense. I suggest inserting lower Suspense boundaries add loading states lower in the hierarchy.

How can I do this? Or where can I find examples?

@zobweyt
Copy link

zobweyt commented Aug 29, 2024

How can I do this?

To handle this properly, wrap user() call in <Suspense> boundaries like below:

import { createSignal, createResource, Show, Suspense } from 'solid-js';

const fetchUser = async (id: number) =>
  (await fetch(`https://swapi.dev/api/people/${id}/`)).json();

export default function Select() {
  const [userId, setUserId] = createSignal<number>();
  const [user, { refetch }] = createResource(userId, fetchUser);

  return (
    <>
      <input
        type="number"
        min="1"
        placeholder="Enter Numeric Id"
        onInput={(e) => setUserId(Number(e.currentTarget.value))}
      />

      <button onClick={refetch}>Refetch</button>

      <div>
        <Show when={user.loading}>
          <p>Loading...</p>
        </Show>

        {/* Wrap `user()` in `<Suspense>` */}
        <Suspense>
          <pre>{JSON.stringify(user(), null, 2)}</pre>
        </Suspense>
      </div>
    </>
  );
}

Without <Suspense>, when you fetch data and it’s not immediately available, the component may render with an empty state, leading to a flicker effect when the new data finally arrives. This flickering happens because the component re-renders as soon as the data is fetched.

Or where can I find examples?

https://docs.solidjs.com/reference/components/suspense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants