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...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
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:
Or React Query. But yes, separate server fetching from local state.
@stereoplegic
Do you know what are the differences between
swr
andreact-query
?I use both and I have no idea how to pitch the differences 😆
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
This! No need to reinvent the wheel every time you want an API dependent state variable
I have used react query and it is quite good. I didn't know about swr though. Will check this out. Thanks for sharing
I fully agree. Don't reinvent the wheel. There's also URQL if you're dealing with GraphQL
formidable.com/open-source/urql/do...
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!!
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.
Very clear your explanation Bradley, got it! Thanks so much!
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!
Thanks @antontsvil ! That helped me as well to understand that it is possible! great example!
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.
Wrong article to post your advertisement sir!
Nice🖤
Two line shayari in Marathi
Beautiful
Motivational Quotes in Marathi
Best 💟
50+ WhatsApp Status in Hindi
Nice🖤
Two line shayari in Marathi
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??
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 😇That makes sense!
Thank you for answering🙌
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.
Great post! Thanks for sharing.
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.
Great Post
I think Yogesh Chavan must use this when he create another tutorial next time.. 😀
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?
Good catch! It was a type.. fixed it 😇
Thank you 🙌
What do you think about calling API directly inside of reducer functions?
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!