Skip to content

Suspense example in docs is incorrect #9019

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

Closed
mpiroc opened this issue Apr 14, 2025 · 4 comments · Fixed by #9119
Closed

Suspense example in docs is incorrect #9019

mpiroc opened this issue Apr 14, 2025 · 4 comments · Fixed by #9119

Comments

@mpiroc
Copy link

mpiroc commented Apr 14, 2025

Describe the bug

A user on discord noticed that the fallback never renders for this example: https://tanstack.com/query/latest/docs/framework/react/examples/suspense

I believe this example is wrong. On the initial render, React.Suspsense's content successfully renders, it just renders null. So when Project or Projects suspend, react continues displaying the most recent successful render (null) until data has been fetched.

To fix this, move the null case outside of the React.Suspense component:

{showProjects ? (
  <React.Suspense fallback={<h1>Loading projects...</h1>}>
    {activeProject ? (
      <Project
        activeProject={activeProject}
        setActiveProject={setActiveProject}
      />
    ) : (
      <Projects setActiveProject={setActiveProject} />
    )}
  </React.Suspense>
) : null}

Your minimal, reproducible example

Original example:
https://tanstack.com/query/latest/docs/framework/react/examples/suspense?panel=sandbox

Fixed example:
https://stackblitz.com/edit/tanstack-query-4yma8xjz?file=src%2Findex.tsx

Steps to reproduce

  1. Navigate to this example in your browser: https://tanstack.com/query/latest/docs/framework/react/examples/suspense
  2. Click on the "Interactive Sandbox" tab
  3. Click on the "Show projects" button

Expected: The fallback (Loading projects...) is rendered while the query is loading
Actual: null is rendered while the query is loading.

Expected behavior

As a developer, I expected the fallback (Loading projects...) to render. Instead, nothing (null) was rendered.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OS: MacOS
Browser: Chrome
Browser Version: 135.0.7049.85

Tanstack Query adapter

react-query

TanStack Query version

v5.74.3

TypeScript version

5.8.3

Additional context

No response

@xxziiko
Copy link
Contributor

xxziiko commented May 7, 2025

Hi @mpiroc, thanks for the clear issue and solution. I noticed this hasn't been updated for a few weeks.
Would you mind if I pick this up and create a PR for it?

@mpiroc
Copy link
Author

mpiroc commented May 7, 2025

@xxziiko Go for it!

@xxziiko
Copy link
Contributor

xxziiko commented May 7, 2025

@mpiroc Thanks! I'll get started on a PR for this and will link it here once it's ready.

xxziiko added a commit to xxziiko/query that referenced this issue May 7, 2025
The previous suspense example incorrectly rendered null instead of the
fallback UI. This change moves the null check outside the Suspense
component to ensure the fallback is displayed as expected.

Fixes TanStack#9019
TkDodo pushed a commit that referenced this issue May 8, 2025
…#9119)

The previous suspense example incorrectly rendered null instead of the
fallback UI. This change moves the null check outside the Suspense
component to ensure the fallback is displayed as expected.

Fixes #9019
@injae-kim
Copy link

fix PR: #9119 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants