DEV Community

Discussion on: useReducer instead of useState while calling APIs!

Collapse
 
diogo405 profile image
Diogo Goncalves

I'd say use swr (swr.vercel.app/) when calling APIs, it returns the data, error, and if it's loading (plus cache, deduplication, etc). Here's a snippet from their website:

import useSWR from 'swr'

function Profile() {
  const { data, error } = useSWR('/api/user', fetcher)

  if (error) return <div>failed to load</div>
  if (!data) return <div>loading...</div>
  return <div>hello {data.name}!</div>
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
stereoplegic profile image
Mike Bybee • Edited

Or React Query. But yes, separate server fetching from local state.

Collapse
 
rexgalilae profile image
RexGalilae

This! No need to reinvent the wheel every time you want an API dependent state variable

Collapse
 
flexbox profile image
David Leuliette 🤖 • Edited

@stereoplegic
Do you know what are the differences between swr and react-query?

I use both and I have no idea how to pitch the differences 😆

Thread Thread
 
stereoplegic profile image
Mike Bybee

Tanner Linsley (author of React Query) details the differences here (also comparing Apollo and RTK Query) much better than I can: react-query.tanstack.com/comparison

Collapse
 
hey_yogini profile image
Yogini Bende

I have used react query and it is quite good. I didn't know about swr though. Will check this out. Thanks for sharing

Collapse
 
binajmen profile image
Benjamin

I fully agree. Don't reinvent the wheel. There's also URQL if you're dealing with GraphQL
formidable.com/open-source/urql/do...

Some comments have been hidden by the post's author - find out more