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

Infinite loop when fetching data with useAsync() #9

Closed
jaybe78 opened this issue Jun 1, 2022 · 5 comments
Closed

Infinite loop when fetching data with useAsync() #9

jaybe78 opened this issue Jun 1, 2022 · 5 comments

Comments

@jaybe78
Copy link

jaybe78 commented Jun 1, 2022

I'm using client routing in vps, and when switching to a page that has a component with useAsync(), I can see in useSsrData, that (obviously) it creates a new key that is different than the initial key created at first load(server side).

Thus, the data in the html tree is never found, then creates a new promise, throws the state, that triggers the update of suspense, which triggers the update of the wrapped component, and so there's an infinite loop... :

UseAsync client side if id has not been created server side

  1. Creates new ID
  2. Id not found
  3. New promise
  4. Update promise State 'pending'
  5. Result: Component unmount/mount
  6. Go back to 1

I think the documentation should make it clear, that useAsync only works when you are coming from the server, so only for server routing, otherwise only useSSRData is indicated with a static id.

  • git clone git@github.com:jaybe78/vps-webpack5-redux-streaming-test.git
  • npm install
  • npm run dev
Screen.Recording.2022-06-02.at.00.08.52.mov

Steps:

  1. Click on About
  2. Go back to Home
  3. Result: infinite loop

I tried with react-router as well using the example you added in vite-plugin-ssr. I get the same issue this time on first load. https://github.com/jaybe78/vps-webpack5-redux-streaming-test/pull/1/files

Though I confirm in both cases it works, if I replace

const movies = useAsync(async () => {
    const response = await fetch(
      "https://star-wars.brillout.com/api/films.json"
    );
    return response.json();
  });

by

const movies = useSsrData('movies', async () => {
    const response = await fetch(
      "https://star-wars.brillout.com/api/films.json"
    );
    return response.json();
  });
@brillout
Copy link
Owner

brillout commented Jun 1, 2022

It should actually work with Client Routing.

Reproduction is welcome and I'll have a look.

@jaybe78
Copy link
Author

jaybe78 commented Jun 1, 2022

@brillout Done ! Thanks

@brillout
Copy link
Owner

brillout commented Jun 2, 2022

I could reproduce, I'll have a look at it tomorrow.

@brillout
Copy link
Owner

brillout commented Jun 3, 2022

It's a React bug: facebook/react#24669.

@jaybe78
Copy link
Author

jaybe78 commented Jun 3, 2022

Oh sh*t! . I guess I'll stick to static id for now.
Thanks @brillout.

@jaybe78 jaybe78 closed this as completed Jun 3, 2022
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