enable/disable SWR #876
-
Alright so I am not sure if currently there is a way to do it, but I noticed some times I need to do some ugly things to prevent SWR to fetch in the first render. Take the next example of a search bar.
That would trigger a request in the first render. Whereas something like this wouldn't
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@scucchiero const [query, setQuery] = useState("")
const { data: results } = useSWR(query ? '/search' + query : null, fetcher) |
Beta Was this translation helpful? Give feedback.
-
React-Query has a great concept of "enabled" queries. Disabled queries will never fetch but they are able to see the data loaded with the same key. It comes in handy in many situations. E.g. you have a stack of
and now you can disable authentication just by commenting out the ROOT In SWR you can disable That's one of the multiple reasons I switched from SWR to React-Query 😅 |
Beta Was this translation helpful? Give feedback.
@scucchiero
What about using the conditional fetch?
https://swr.vercel.app/docs/conditional-fetching#conditional