DEV Community

Cover image for useReducer instead of useState while calling APIs!

useReducer instead of useState while calling APIs!

Yogini Bende on April 27, 2021

Hello folks! It’s been a while since React has introduced Hooks and we all fell in love with it’s patterns and ease of use. Though this is the ca...
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
 
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
 
rexgalilae profile image
RexGalilae

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

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...

Collapse
 
jharteaga profile image
Jose Arteaga

Great post! it helped a lot to understand much better useReducer. Now, I'll try to put it into practice to remember its usage. Just quick question. Can I have more than one useReducer inside the Function Component? Because in the example there is just one and you call the dispatch method, however if you have more than one useReducer, how I can make the difference which I am working with? Thanks!!

Collapse
 
bpitts8019 profile image
Bradley Pitts

While it is possible to use more than one useReducer for the state of a component. I would recommend against it as each use of the useReducer hook will return it's own state object and dispatcher. Instead design the single reducer to control the whole state of the component. Any defined action in the reducer should return the whole state of the component with any expected updates as per that defined action.

Collapse
 
jharteaga profile image
Jose Arteaga

Very clear your explanation Bradley, got it! Thanks so much!

Collapse
 
antontsvil profile image
Anton T

To help understand hooks, you might wanna look up array destructuring (similar to object destructuring). In short - you can use whatever names you like when destructuring an array, so just use different names for your second useReducer hook!

const [state, dispatch] = useReducer(userDetailsReducer, initialState);
const [likeState, likesDispatch] = useReducer(userLikesReducer, initialLikesState);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jharteaga profile image
Jose Arteaga

Thanks @antontsvil ! That helped me as well to understand that it is possible! great example!

Collapse
 
esigaraantalya7 profile image
Info Comment hidden by post author - thread only accessible via permalink
Elektronik Sigara Antalya

Antalya elektronik sigara bayisi olarak son güncel teknolojiyi takip ediyor ve sağlam işe yarayan ürünleri sizlere sunuyoruz. Ekibimiz bu konuda bir hayli mesai harcamakta ve pod mod Antalya Antalya firması olarak olabildiğince hızlı kargo çıkarmaktayız. En iyi pod mod elektronik sigara modelleriniz Antalya merkez dahil tüm ilçelere dağıtım yapmaktayız.

Collapse
 
hey_yogini profile image
Yogini Bende

Wrong article to post your advertisement sir!

Collapse
 
ravirajtambile profile image
Info Comment hidden by post author - thread only accessible via permalink
Ravirajtambile
Collapse
 
ravirajtambile profile image
Info Comment hidden by post author - thread only accessible via permalink
Ravirajtambile
Collapse
 
yukiyohure profile image
Yuki Shibata

Thank you for the awesome post!
This makes my understanding of the useReducer deepen.

I've wondered that Is that a usual way to define an API-call function IN the useEffect hooks??

Collapse
 
hey_yogini profile image
Yogini Bende

If you don't need to use it outside the hook, there is no harm in declaring it inside. The only purpose of declaring a function there is to enable async-await. But again.. its our choice 😇

Collapse
 
yukiyohure profile image
Yuki Shibata

That makes sense!
Thank you for answering🙌

Collapse
 
abiolaajibola profile image
Abiola

Nice and informative post!

I would also want to avoid using useReducer to manage local state if I am using Redux for 'global' state management, it obviously can get confusing.

Collapse
 
rmaurodev profile image
Ricardo

Great post! Thanks for sharing.

Collapse
 
lizardkinglk profile image
sndp

Great post. Simple and Nice. Btw, I always check if async await call returned data and ready to render because sometimes it says UserDetails.map is not a function. This way very helpful.

Collapse
 
hashimlokasher profile image
Hashim Lokasher

Great Post

Collapse
 
z4ck987 profile image
Z4ck987

I think Yogesh Chavan must use this when he create another tutorial next time.. 😀

Collapse
 
spyda247 profile image
Babajide Ibiayo

Excellent Article...thanks for sharing.

I noticed you dispatched a action of type ACTIONS.SUCCESS in the event of an Error response status. Is this a typo?

Collapse
 
hey_yogini profile image
Yogini Bende

Good catch! It was a type.. fixed it 😇
Thank you 🙌

Collapse
 
peterwd1 profile image
Peter

What do you think about calling API directly inside of reducer functions?

Collapse
 
rubenruvalcabac profile image
Ruben Ruvalcaba

Great tip. I've faced many times updating more than one state and never felt well updating each one separately. This technique is lengthy but cleaner and prioritize separatation of concerns.
Thanks!

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