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 #58

Closed
rotexhawk opened this issue Nov 1, 2019 · 3 comments
Closed

Suspense Mode #58

rotexhawk opened this issue Nov 1, 2019 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@rotexhawk
Copy link

I noticed two problems with the suspense mode.

  1. The default example useSWR('/api', fetch, { suspense: true }) doesn't work because it returns a readable stream.

  2. When I modified the default example and returned data, I noticed, the component was rendered multiple times.

import React from 'react';
import useSWR from 'swr'

const FetchSuspense = endpoint => {
    console.log('calling me with', endpoint)
    return fetch(endpoint).then(data => data.json()).then(res => res.results[0])
}

export const Person = ({ resource, num  }) => {
     const { data } = useSWR('https://randomuser.me/api', FetchSuspense, { suspense: true })
     console.log("calling comp", num)
     return (<div>Loaded {num}: - {data.name.first}</div>)
};
@icyJoseph
Copy link
Contributor

icyJoseph commented Nov 1, 2019

Yes, I think the docs might be misleading when using the word fetch, as you are supposed to make your own handler, as oppose to pass window.fetch. The examples are very well done though. Maybe a PR is worth the effort?

As for the second part, it happens because there's a few other values coming out from the hook, and in this case the isValidating prop triggers this re-render (there's also the error prop and the revalidate function).

import React, { Suspense } from "react";
import useSWR from "swr";

export function useWordPromise(word) {
  const { data, ...rest } = useSWR(
    word,
    arg => {
      console.log("calls promise handler");
      return new Promise(resolve => resolve(arg));
    },
    {
      suspense: true
    }
  );

  console.log("calls hook", rest);
  return data;
}

function Word() {
  const word = useWordPromise("the word");
  return <p>{word}</p>;
}

function App() {
  return (
    <Suspense fallback={<p>Loading Word</p>}>
      <Word />
    </Suspense>
  );
}

When I run the above, I see three calls hook, the initial pass, when isValidating is true, and finally when isValidating becomes false (we got data), but only one calls promise handler. Remember too that this library does re-fetch on auto focus.

@shuding shuding added the documentation Improvements or additions to documentation label Nov 3, 2019
@shuding
Copy link
Member

shuding commented Nov 3, 2019

Thank you! We'll make the documentation about fetch clearer.

The second question should be related to #57.

@shuding shuding closed this as completed Nov 6, 2019
@shuding
Copy link
Member

shuding commented Nov 6, 2019

Should be fixed in swr@0.1.8.

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

No branches or pull requests

3 participants