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

Suspense mode with useTransition mutate not work expectly #4113

Open
xiaoxiyao opened this issue Mar 31, 2025 · 2 comments
Open

Suspense mode with useTransition mutate not work expectly #4113

xiaoxiyao opened this issue Mar 31, 2025 · 2 comments

Comments

@xiaoxiyao
Copy link

xiaoxiyao commented Mar 31, 2025

Bug report

Description / Observed Behavior

Image

Pending state flashed briefly, expected to persist for 1 second.

Repro Steps / Code Example

https://codesandbox.io/p/sandbox/swr-suspense-wrzxgn

Additional Context

SWR version: 2.3.3

@angeeeeelh
Copy link

Hello @xiaoxiyao,
I do not see a bug with SWR. The way this code is using SWR and React hooks is behaving as I expect.

  1. The code is using React version 18.x.x, which means the "useTransition" hook requires synchronous updates
  2. I also don't recommend mutate being used with useTransition for what I think you want. SWR will not allow you to mock a delayed fetching of data easily because it's supposed to fetch (cached or new) data fast.

Here's code I wrote of what I think you're looking for.

@xiaoxiyao
Copy link
Author

xiaoxiyao commented Apr 7, 2025

Thanks @angeeeeelh , initially I wanted to wrap mutate with useTransition to avoid triggering suspense repeatedly. Later, I realized that mutate does not trigger suspense, and I can directly use isValidating to determine whether data is being re-fetched.

I wonder if this means that SWR's suspense mode only triggers suspense during the initial data fetch, and subsequent mutate or revalidation is always a "transition". Therefore, SWR neither needs nor can work with useTransition.

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

No branches or pull requests

2 participants