-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
With React 18RC, onSuccess does not get called when using Suspense #1733
Comments
@promer94 I get the same error with your codesandbox example in both react 17 and 18 (as you have Suspense set to true). I do notice that if I click on the browser window a bunch that the console output does appear. I'm assuming that has something to do with onfocus events in SWR but that's not really relevant to my issue, which is onsuccess should be called immediately on completion of the promise. Here attached is a video that shows on success not being called in codesandbox. I tried it in both chrome and safari with the same results. react-18-not-called-on-success-CRA.mp4 |
After reviewing the code. It might be hard to change the current behaviour. In non-suspense mode
But in suspense mode
Beacuse we want to help user to avioid the problems like #286 #787, the config callback will only be called if the hook is mounted. Could you share your real world use cases with us ? |
Example Repo I will use in my course with non-working onSuccess: onSuccess code here: My real world problem is that I have a list of cities that I load inside a suspense boundary and after the list is loaded, I want to select the first item in the list returned so that other components on the page can react and render. I'm doing this for a Pluralsight Course that includes Suspense so it's important that I provide a best practices example of how to do this. I was hoping I could do it directly with suspense, but I have no idea how to capture the completion of the returned promise that is inside my suspense boundary. I've tried useEffect, but without success. I was hoping that the return of what gets passed into startTransition would get executed at completion but that does not work either. |
Also, is there a way I can set SWR to always make a fresh request and never use the cache? That would be more desirable for teaching Suspense. I've tried setting all the option as aggressively as I could but the cache still happens. I'm on my mobile right now so can't provide options I set trying to disable caching. |
@promer94 , I've been trying to solve this on my own. I'm still not seeing how I can work with Let me know if you need a better simpler example then then my full teaching app. Below is my attempt that works some of the time, but clearly not the final answer.
|
pkellner/pluralsight-react-18-suspense-swr-problem#1 |
Thanks for trying @promer94 but I'm still not getting the behavior I'm wanting. That is, the first city being selected after the citylist downloads. Take a look at the "React 17" version at the URL to see what I mean. That is the one that has the onSuccess working. I'm trying to do the same thing with React 18 and suspense true. |
Thanks @promer94 Your second pull request did achieve my goal. I hope there is a clean long term solution for using useSwr and suspense. It does feel like it's a step backward to move to suspense to achieve what was a lot easier before suspense was added. Also, I add the Thanks for sticking with me on this.
|
Thanks for all the feedback! As you know that currently suspense is still experimental and we’re still exploring the best practices with SWR for the various use cases. |
The below example is running with the
package.json
referenced at the bottom of this issue.The code following works as expected which is not using suspense, but using React 18. That is, YES is shown in the rendered output.
This code, however, has NO remaining showing that onSuccess was never called.
package.json
follows:The text was updated successfully, but these errors were encountered: