DEV Community


Discussion on: useReducer instead of useState while calling APIs!

diogo405 profile image
Diogo Goncalves

I'd say use swr ( 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 {}!</div>
Enter fullscreen mode Exit fullscreen mode
stereoplegic profile image
Mike Bybee • Edited

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

rexgalilae profile image

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

flexbox profile image
David Leuliette 🤖 • Edited

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:

ms_yogii profile image
Yogini Bende Author

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

binajmen profile image

I fully agree. Don't reinvent the wheel. There's also URQL if you're dealing with GraphQL

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

Forem Open with the Forem app