Infinite loop when fetching data with `useAsync()`
jaybe78 opened this issue · comments
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
- Creates new ID
- Id not found
- New promise
- Update promise State 'pending'
- Result: Component unmount/mount
- 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:
- Click on
About
- Go back to
Home
- 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();
});
It should actually work with Client Routing.
Reproduction is welcome and I'll have a look.
I could reproduce, I'll have a look at it tomorrow.
It's a React bug: facebook/react#24669.