Skip to content

useLoader and React 19.2.3 Suspense aren't working correctly togather #87

@speyar

Description

@speyar

Suspense is loading infinitely when i use useLoader on a component and wrap it with Suspense. The image gets loaded, but the the scene isn't redrawn until i make trigger a rerender or resize. i had to come up with something like this for it to work

function useTextureLoader(url: string) {
  const gl = useOGL((state) => state.gl);
  const [texture, setTexture] = useState<OGL.Texture | null>(null);
  const [promise, setPromise] = useState<Promise<void> | null>(null);

  useEffect(() => {
    const img = new Image();
    const loadPromise = new Promise<void>((resolve) => {
      img.onload = () => {
        const t = new OGL.Texture(gl);
        t.image = img;
        setTexture(t);
        resolve();
      };
    });

    setPromise(loadPromise);
    img.src = url;
  }, [gl, url]);

  if (!texture && promise) {
    throw promise;
  }

  return texture;
}

I'm using NextJS 16.1.0 with React 19.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions