Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

useSWR for mutiple requests

I'd like to implement useSWR in my web app and remove axios from the project, but I don't see how I can use SWR multiple times on the same page. I thought I could use two hooks:

const { userData, userError } = useSWR(url, fetcher)

const { coinData, coinError } = useSWR(url,fetcher)

but that doesn't work. (I guess you need to use { data, error })

This is the code I currently have, but I'd like to make another get request to my database with SWR.

  const fetcher = (...args) => fetch(...args).then((res) => res.json());

  const { data, error } = useSWR(
    `https://api.coingecko.com/api/v3/coins/${coinURL}`,
    fetcher
  );

  if (error) {
    return <div>failed</div>;
  }
  if (!data) {
    return <div>Loading</div>;
  }
  return <div>{data.name}</div>;

What's the best way to make multiple requests on the same page with useSWR?

like image 928
Eric Pezzulo Avatar asked Oct 12 '25 03:10

Eric Pezzulo


1 Answers

try

const { data : userData, error: userError } = useSWR(url, fetcher)

const { data : coinData, error: coinError } = useSWR(url,fetcher)
like image 120
Chemi Adel Avatar answered Oct 14 '25 20:10

Chemi Adel