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 is not resolved and the requests are looping #46

Closed
NicholasKuchiniski opened this issue Oct 30, 2019 · 6 comments · Fixed by #49
Closed

Suspense is not resolved and the requests are looping #46

NicholasKuchiniski opened this issue Oct 30, 2019 · 6 comments · Fixed by #49
Labels
bug Something isn't working

Comments

@NicholasKuchiniski
Copy link

NicholasKuchiniski commented Oct 30, 2019

import React, { Suspense } from 'react';
import useSWR from '@zeit/swr'

function StarWarsSuspense() {
  return (
    <Suspense fallback={<div>loading ...</div>}>
      <StarWars />
    </Suspense>
  )
}

function StarWars() {
  const { data } = useSWR('http://swquotesapi.digitaljedi.dk/api/SWQuote/RandomStarWarsQuote', fetch, { suspense: true });

  return (
    <div>
      {data.starWarsQuote}
    </div>
  );
}

export default StarWarsSuspense;

With this code, i never get the final component, the placeholder stay forever, looking on chrome developer console, the requests are looping.

I made this code using the readme example, i tried change de fetch function to a random promise to see if i get out of fallback, without no results.

I doing something wrong or this is really something that should not happen ?

@erikjung
Copy link

I also get the same outcome: https://codesandbox.io/s/lucid-field-kbz20

Not sure if the suspense option is intended for the existing suspense feature in React stable or for the new concurrent functionality of react@experimental. Tried both approaches with both versions of React and got the same results.

Also unclear if anything special is required for the fetch function implementation. The docs imply that passing the global fetch function directly should work, but this basic example is wrapping fetch to resolve the .json() promise as well. 🤷‍♂️

@shuding shuding added the bug Something isn't working label Oct 30, 2019
@shuding
Copy link
Member

shuding commented Oct 30, 2019

@NicholasKuchiniski @erikjung

This is indeed a bug due to the deduplication related code. We're working on a fix.

@shuding
Copy link
Member

shuding commented Oct 30, 2019

Regarding the fetch, sorry for the unclear information 🙏

We just added a message to the Quick Start:

Note that fetch can be any asynchronous function, so you can use your favourite data-fetching library to handle that part.

Also in the API section:

Promise returning function to fetch your data

@shuding
Copy link
Member

shuding commented Oct 31, 2019

Should be fixed in 0.1.2 🙏

@erikjung
Copy link

Thanks @quietshu

Still seeing the same issue with 0.1.2 for some reason 🤔

https://codesandbox.io/s/swr-playground-ku9mq

Took fetch out of the equation and used the unit test itself, but still never resolves:

function Section() {
  const { data } = useSWR(
    "suspense-1",
    () => new Promise(res => setTimeout(() => res("SWR"), 100)),
    {
      suspense: true
    }
  );
  return <div>{data}</div>;
}

function App() {
  return (
    <div className="App">
      <Suspense fallback={<div>Loading</div>}>
        <Section />
      </Suspense>
    </div>
  );
}

This is w/ react@16.11.0 also.

@shuding
Copy link
Member

shuding commented Oct 31, 2019

@erikjung sorry that PR wasn't included in 0.1.2.
Updating to swr@0.1.5 will fix it:

https://codesandbox.io/s/swr-playground-946db

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants