How to fetch multiple times in single hook?
See original GitHub issueIt 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:
- Created 4 years ago
- Comments:8 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The problem is in
useMultipleFetch.js
(which is like redux’sstates are equal
), it can be solved by using the following codes inthen
:BTW, if the
results
andisLoadings
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.@shafiemukhre
I guess you’d like to execute the callback in
useEffect
only once (like thecomponentDidMount
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 toids
(likecomponentDidMount
+componentDidUpdate
in class component), just keep this code, and check the outer code which callsuseConversation
, make sure theids
wont’ be changed afteruseConversation
. A possible way is: do not use[names, dates, times]
to calculate the newids
, 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 callsetNames
,setDates
,setTimes
only once afterfetch
. Operator...
has O(N) time complexity, if you execute N times, it’ll cost O(N^2), which means if you have 30 items inids
, you’ll get about 30*30*3/2=1350 temp items.