Next.js Discord

Discord Forum

SWR | How do you use `isValidating` in multiple SWR calls within a single functional component?

Answered
Atlantic menhaden posted this in #help-forum
Open in Discord
Atlantic menhadenOP
Issue: I have a dashboard component that shows open and completed orders. I have to call two different endpoints (one for getting the open orders and one for getting the completed orders). I would like to use isValidating to check the state of both API responses and show a loading animation in the UI for each section of the dashboard if isValidating is true.

My question is, how do you use isValidating in multiple SWR calls within a single functional component?

What is shown below obviously does not work because you can't re-declare a const. I have not come across any examples in the SWR docs of using isValidating with multiple calls within the same component.

You can see a snapshot of my code here: https://stackoverflow.com/questions/76886562/how-to-use-isvalidating-in-multiple-swr-calls-in-the-same-component

Any help would be greatly appreciated!
Answered by European sprat
yeah but you can just do what you did with openOrders and completedOrders
View full answer

4 Replies

European sprat
Can't you make them isValidating1 and isValidating2?
Atlantic menhadenOP
@European sprat No, the name of the hook is isValidating.

//Endpoint #1 const { data: openOrders, error: openOrdersError, isValidating, } = useSWR([openOrdersEndpoint, BASE_URL], ([url, audience]) => fetchData(url, audience) );

//Endpoint #2 const { data: completedOrders, error: completedOrdersError, isValidating, } = useSWR([completedOrdersEndpoint, BASE_URL], ([url, audience]) => fetchData(url, audience) );
European sprat
yeah but you can just do what you did with openOrders and completedOrders
Answer
European sprat
isValidating: isValidatingOpenOrders