question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetching for each data in collection

See original GitHub issue

When you have a dependent call and you want to make a call for each of the data in the collection, how does you handle that with swr?

  const { data: users } = useSWR<Response<User[]>>(
    [usersURL(), token],
    request,
    { revalidateOnFocus: false },
  );

  const allProfiles = [];
  if (users) {
    users .data.forEach(user => {
      const { data: profiles } = useSWR<Response<Profile[]>>(
        [userProfileURL(user.id), token],
        request,
        { revalidateOnFocus: false },
      );

      if (profiles) {
        allProfiles.push(profiles);
      }
    });
  }

The above code snippet does not work, I am getting errors like this Rendered fewer hooks than expected.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
pacocourseycommented, Dec 5, 2019

Looks like this has been solved!

For future reference, the error

Rendered fewer hooks than expected.

Is caused by using hooks under a conditional:

if (users) {
  // ...
  useSWR(...) // breaks the rules of react hooks
}

Additionally, you can do dependent fetching as described here: https://github.com/zeit/swr#dependent-fetching

0reactions
fa10commented, Dec 3, 2019

@sergiodxa this is the solution I went for!

const fetchSomething = async (id: string, token: string) => {
  try {
    const { data: collection } = await request(
      `${id}/data`,
      token,
    );

    const responses = await Promise.all(
      collection.map(site =>
        request(/data, token),
      ),
    );

    return { data: flatten(responses .map(response => response.data)) };
  } catch (error) {
    return { data: undefined, error };
  }
};

Then use the fetchSomething like this:

  const { data } = useSWR(
    [id, token],
    fetchSomething,
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrieving Elements from Collection in Java (For-each, Iterator ...
Cursor is an interface and it is used to retrieve data from collection object, one by one. Cursor has 3 types, which are...
Read more >
Fetching for each data in collection #156 - vercel/swr - GitHub
When you have a dependent call and you want to make a call for each of the data in the collection, how does...
Read more >
Fetch documents from collection in MongoDB - w3resource
Fetch all data from the collection ... If we want to fetch all documents from the collection the following mongodb command can be...
Read more >
MongoDB Show all contents from all collections - Stack Overflow
To show all collections content or data use below listed code which had been posted by Bruno_Ferreira. var collections = db.getCollectionNames(); for(var i ......
Read more >
Fetch all data from two different collection using single query
Hello… can you help me with a query that can fetch all data from two different collections in a single query. consider example:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found