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.

How to fetch multiple times in single hook?

See original GitHub issue

It seems that I can’t fetch multiple APIs in a single hooks and use arrays to return values.

Code Sandbox: https://codesandbox.io/s/ykly7w7w9x

Say I have components which need to fetch data with several APIs, I would like to abstract the hook into a single useMultipleFetch. But the problem is state would not update after fetching finished. I must manually update the state by resize the window to force states update.

expected :

Fetch result (expected 3 `done`)
["done","done","done"]
isLoadings result (expected all `false`)
[false,false,false]

but got:

Fetch result (expected 3 `done`)
["","",""]
isLoadings result (expected all `false`)
[true,true,true]

for the first time.

BTW use a single fetch hook would avoid this problem, so I guess it could be that React can’t handle state as a nested array.

Is this the wrong way I use it and is it not supported by React hooks? Or is there any other thing wrong?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
RexSkzcommented, Apr 6, 2019

The problem is in useMultipleFetch.js (which is like redux’s states are equal), it can be solved by using the following codes in then:

setIsLoadings([...isLoadings]);
setResults([...results]);

BTW, if the results and isLoadings is really changed, there’ll be an infinite loop in your code, because the changes will cause re-render and then fetch/change data again.

0reactions
RexSkzcommented, Apr 23, 2020

@shafiemukhre

I guess you’d like to execute the callback in useEffect only once (like the componentDidMount in class component). In this case, // eslint-disable-line react-hooks/exhaustive-deps seems ok for me, because the comment will remind me that “this callback won’t be executed again, useConversation is not a reactive function”.

If you’d like to make useConversation reactive to ids (like componentDidMount+componentDidUpdate in class component), just keep this code, and check the outer code which calls useConversation, make sure the ids wont’ be changed after useConversation. A possible way is: do not use [names, dates, times] to calculate the new ids, just keep them in your component and use them in JSX, ids should be changed only in necessary.

P.S. To avoid performance issue, using batch API such as fetch(url, { params: ids }), then call setNames, setDates, setTimes only once after fetch. Operator ... has O(N) time complexity, if you execute N times, it’ll cost O(N^2), which means if you have 30 items in ids, you’ll get about 30*30*3/2=1350 temp items.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple fetch data axios with React Hooks - Stack Overflow
I try to make it with async await but It's is correct? I've got 4 times reRender (4 times console log). It is...
Read more >
Make Fetch(s) Happen. Handling Multiple Fetch Requests in…
If you need to call two endpoints at the same time, utilizing Promise.all is a quick and efficient way to make a call...
Read more >
Handling multi-page API calls with React Hooks
First lets create a custom hooks file called useFetchGames.js in a helpers directory to handle fetching our data from CheapShark. This custom ...
Read more >
React hooks... Oops! Part 2 - why does my effect run multiple ...
Every time the component renders, React checks if all the values in the deps array are still the same. If any of them...
Read more >
How to Fetch Data From an API Using React Hooks
We can now call our fetchPost function in a useEffect hook. This hook will take in two parameters: the first one is the...
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