Next.js Discord

Discord Forum

What is the difference between using a function in the SWR key parameter and a simple string?

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
const { data } = useSWR(shouldFetch ? '/api/data' : null, fetcher)
const { data } = useSWR(() => shouldFetch ? '/api/data' : null, fetcher)

On both examples illustrated above, useSWR will have a ternary as a key and will return a string if the condition is true. The first example, however, does not have a function while the second does.
What is the difference?

I took this example from the SWR main docs: https://swr.vercel.app/docs/conditional-fetching

I am guessing that they just wanted to illustrate the possibility of using a function in the key parameter, but there's no explicit indication.

0 Replies