Тёмный

How to Fetch Data in React With A Custom useFetch Hook 

PedroTech
Подписаться 214 тыс.
Просмотров 78 тыс.
50% 1

In this video I will show how to make a custom useFetch hook in react. The hook allows you to fetch data from an api both on render and lazily.
API: v2.jokeapi.dev...
Code: github.com/mac...
🚀 Learn ReactJS By Building 6 Projects: codedamn.com/l...
Social
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
Website: machadopedro.com
Linkedin: / machadop1407
Instagram: / _pedro.machado_
Github: github.com/mac...
Email: machadop1407@gmail.com
Tags:
- ReactJS Tutorial
- ReactJS and MySQL
- NodeJS Tutorial
- API Tutorial

Опубликовано:

 

26 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 177   
@0x0abb
@0x0abb 2 года назад
awesome work again - i am 52 years old - i am training to become a blockchain dev but now i am hooked on react which is still essential for blockchain work - i am inspired by guys like yourself - you guys are genius!!
@waifufx
@waifufx 2 года назад
Well done sir
@mohshinmostafa3900
@mohshinmostafa3900 2 года назад
You are awesome too!
@saugatrajbhandari4247
@saugatrajbhandari4247 2 года назад
These type od comments motivates me
@aryankumar87771
@aryankumar87771 Год назад
not worth it learning it at 52, seriously not worth it..... unless you're totally insane or 1 in a million genius quick learner
@Uixxxam
@Uixxxam Год назад
@@aryankumar87771 Let other people do what they want to do
@_boza
@_boza 2 года назад
You are one of the RU-vidrs who post real-life coding stuff that really helps! Thank you! :)
@blockanese3225
@blockanese3225 3 года назад
Thank you for teaching me react! I’ve been watching your videos for a few months now and have learned more here than I have in college.
@BobbyBundlez
@BobbyBundlez 3 года назад
yet all these jobs want college graduates. this whole industry is honestly retarded these days.
@kevyyar
@kevyyar 2 года назад
They tech react in college?
@blockanese3225
@blockanese3225 2 года назад
@@kevyyar yes and angular.
@ashishkumawat6110
@ashishkumawat6110 Год назад
The best explanation of using a custom hook so far... Hats off man.
@capitaoisma
@capitaoisma 2 года назад
Thank you very much for the video, after struggling to fetch some data from an API that I created, finally nailed it thanks to your help!!!!!
@Shaunmcdonogh-shaunsurfing
@Shaunmcdonogh-shaunsurfing 2 года назад
The refetch was gold! Thank you man!
@digender
@digender 2 года назад
This is great. I recently switched from Angular to react and was wondering, how to seperate http from component as in angular we have services, here we have custom hooks to take care of it. Great tutorial bud😃
@Pareshbpatel
@Pareshbpatel 2 года назад
Excellent tutorial on fetching data using a custom react hook. Thanks, Pedro {2022-02-14} , {2022-02-18}, {2023-08-06}
@GabrielSouza-sl8vp
@GabrielSouza-sl8vp 3 года назад
Great vídeo again! It would be interesting to bring some videos of React using Typescript too.
@PedroTechnologies
@PedroTechnologies 3 года назад
Thank you! I will make more ts videos!
@JoaoPiovensan
@JoaoPiovensan 3 года назад
First video that i watch of this channel and already loved it, keep making the good work mate
@PedroTechnologies
@PedroTechnologies 3 года назад
Thank you! Welcome to the channel :)
@wilber3015
@wilber3015 Год назад
Wow! Thanks Pedro, by watching this video in just 16 min I was able to learn a lot about React & APIs
@nehanagda8888
@nehanagda8888 Год назад
Thankyou so much for the explanation. I have watched your video for the first time and you explained really well that I subscribed your channel immediately after the video finished.
@bekzatmurat1341
@bekzatmurat1341 Год назад
Thank you bro. I knew about custom hooks. But i didn't use it. That was really cool. I will be using it on every project from now on.
@michaelchinye7993
@michaelchinye7993 2 года назад
Thanks so much man. I really needed to know how to use custom hooks in callback function and you have just made it easy for me. Thanks
@JuliaMaschion
@JuliaMaschion 2 года назад
I loved your channel! Already subscribed !
@PedroTechnologies
@PedroTechnologies 2 года назад
Thank you so much 🤗
@dane_x_music
@dane_x_music Год назад
Dude, Thanks much, been searching for this for hours!
@Markdim7
@Markdim7 3 года назад
That was a very useful video! I am doing my final project for my degree, which includes some data fetching from facebook and this will be very useful
@PedroTechnologies
@PedroTechnologies 3 года назад
Using a premade hook will improve the code quality! Good luck in the project :)
@KC-hl4oj
@KC-hl4oj 3 месяца назад
Really great tutorial - thanks soooooo very much!
@cassiosalvador7961
@cassiosalvador7961 Год назад
Very good and concise explanation, thanks for that!
@brandonp611
@brandonp611 3 года назад
Thanks so much for the tutorials. They have been helping me alot. The crud tutorial with mysql helped me understand the connection between the client and server files. Thanks keep doing what you do.
@AkashDas-vh4ru
@AkashDas-vh4ru Год назад
Excellent brother! Keep it up.💙
@marinasatsyk2184
@marinasatsyk2184 2 года назад
Tnak tou very mutch! Your explanations helped me a lot!
@bear-code
@bear-code 2 года назад
thank you man , you are legend
@borrokatu3656
@borrokatu3656 2 года назад
you explains conceps very well
@dezinhtang
@dezinhtang 2 года назад
Great Explanation.I love the way you teach
@k303k
@k303k Год назад
thanks a lot for the video pedro!
@FadlySansan
@FadlySansan 3 года назад
thank you so much. I think I can move on from class & redux using this to make action and handle my state
@exacode
@exacode Год назад
Thanks man, this was very good
@_h4x0r
@_h4x0r 2 года назад
Thank you, Pedro!
@ThColinPereira
@ThColinPereira 3 года назад
Exactly what I was looking for!
@denniszheng7827
@denniszheng7827 3 года назад
Thanks for your course, it's helpful.
@piotrszczesniak685
@piotrszczesniak685 2 года назад
awesome tutorial. the only this I didn't get is the notation { date?.setup }
@nauni07
@nauni07 2 года назад
Awesome explanation
@love_hunter_coc
@love_hunter_coc 3 года назад
Your tips helpful for us thank you brother more like this ❤️🙏
@PedroTechnologies
@PedroTechnologies 3 года назад
More to come!
@love_hunter_coc
@love_hunter_coc 3 года назад
@@PedroTechnologies waiting 😍
@franco.delcas
@franco.delcas 2 года назад
Cool stuff! thank u
@johannsebastianbach3411
@johannsebastianbach3411 2 года назад
In fact this is how react-query was invented by Tanner Linsley!
@cybertechzen5411
@cybertechzen5411 2 года назад
Tnx man it really helped keep it up
@balgrimesart564
@balgrimesart564 2 года назад
really nice tutorial! following right now to see more of these 😀
@shashankshekharsingh6076
@shashankshekharsingh6076 2 года назад
Simply lucid 👍🙏🏻
@S--xc4rv
@S--xc4rv 11 месяцев назад
Always precise 🔥, i have a doubt that instead of creating another fxn we can pass a variable as the dependencies of useEffect so whenever the btn got clicked , new joke gets fetched
@agustinperez8700
@agustinperez8700 2 года назад
Nice video brou, I'm going to make my own implementation in typescript for my app
@elinev7830
@elinev7830 3 года назад
Could U explain how to do post or delete call, in the same custom hook?
@havelboumbidi440
@havelboumbidi440 2 года назад
I have been searching for the same thing. have you found anything? if yes could u share please?
@milosnikolic4282
@milosnikolic4282 2 года назад
What a beutiful solution! Great video, it helped alot!
@princenarteh8822
@princenarteh8822 3 года назад
Great video! But what if you want to post data. How can you go by? I will appreciate if you can do a video on it. Thank you.
@PedroTechnologies
@PedroTechnologies 3 года назад
You can do the same thing, but also put an argument to the hook that represents the body of the post request :)
@progremeramatir
@progremeramatir 2 года назад
Very good tutorial
@usmanirevo895
@usmanirevo895 3 года назад
Awesome video, well explained
@PedroTechnologies
@PedroTechnologies 3 года назад
Thank you!
@JasonWee
@JasonWee 2 года назад
ROFLMAO... the api jokes is SOOOOOOOOOOOOOOOOOOOOOOOOO FUNNY
@eliasantagiuliana6379
@eliasantagiuliana6379 2 года назад
I have a double null response before the correct one if for example I console.log the data
@TheNamesJT
@TheNamesJT 3 года назад
Really good video my dude, didn't know you could return a function and call it like that. Very useful keep these react tricks coming. I would like to see some more real life examples of custom hooks so maybe you could make a custom hook series.
@bastienv6233
@bastienv6233 Год назад
Thank again for your Nice content
@yusufahmed3678
@yusufahmed3678 Год назад
I was wondering if this would cause memory leaks when there are 2 or more components using the usefetch hook, but with different API endpoints.
@evanvana1485
@evanvana1485 3 года назад
Great video! What theme are you using in VS Code?
@PedroTechnologies
@PedroTechnologies 3 года назад
Electron theme!
@jobhatti
@jobhatti 3 года назад
Awesome stuff keep it up!
@PedroTechnologies
@PedroTechnologies 3 года назад
Appreciate it!
@zeddscarlxrd4331
@zeddscarlxrd4331 Год назад
Awesome dude thankssss
@sanuyadav-ys3fb
@sanuyadav-ys3fb Год назад
Thank you so much bro. Pleas econtinue to make such video
@anbhuonline
@anbhuonline 2 года назад
Simple and easy 👍
@sonamohialdin3376
@sonamohialdin3376 2 года назад
So good tutorial very useful
@freemenclub
@freemenclub 2 года назад
Great, thanks!
@hamdskid
@hamdskid Год назад
U saved my life 😂😂
@brunomorais4129
@brunomorais4129 2 года назад
how can I make the hook more dynamic by receiving the method by parameter?
@abhaytiwari6411
@abhaytiwari6411 3 года назад
Splendid video 👍
@PedroTechnologies
@PedroTechnologies 3 года назад
Glad you enjoyed it!
@abdoulayendiaye1656
@abdoulayendiaye1656 2 года назад
YOU ARE AMAZINGGGGGGGGGGG
@sumitdhakal6405
@sumitdhakal6405 Год назад
i get this type of error Cannot destructure property 'data' of '(0 , _useFetch__WEBPACK_IMPORTED_MODULE_1__.default)(...)' as it is undefined. TypeError: Cannot destructure property 'data' of '(0 , _useFetch__WEBPACK_IMPORTED_MODULE_1__.default)(...)' as it is undefined.
@Xtopherization
@Xtopherization 2 года назад
thanks for the tutorial and github repo ... you're the best! are you planing on doing an updated one with the async/await with the new features of react 18 ?
@tareq8022
@tareq8022 19 дней назад
is this the best way custom hook or react query or redux-toolkit ?
@Lunolux
@Lunolux 2 года назад
Great video
@devinosborne3396
@devinosborne3396 2 года назад
Great video! How would you recommend making multiple calls in the same component?
@marcossequeira5433
@marcossequeira5433 2 года назад
its not much easy to instance the setLoading in true in the useState instead inside the useEffect?
@diego0ji
@diego0ji 2 года назад
Superr
@TheNamesJT
@TheNamesJT 3 года назад
Hey pedro could you make a video where you use an api that asks for header and rate limits? because can't seem to find a video online that teaches how to follow an api requirements of use. Its all these simple api's that has no prerequisites/restrictions. Basically, how to use an api with oauth2 & rate limiting?
@EK-rp8jp
@EK-rp8jp 3 года назад
super useful! thanks a lot:>
@Dev-Phantom
@Dev-Phantom 3 месяца назад
cool
@divagesh
@divagesh 2 года назад
what if we want to call multiple url /api in a component ?
@aghilannathan8169
@aghilannathan8169 2 года назад
What is the reason to do this instead of using something like React Query?
@JaredFL
@JaredFL 2 года назад
Since you are returning at the end of the useFetch function, your component that calls useFetch will never see the loading state until it is true.
@Dipenparmar12
@Dipenparmar12 2 года назад
Amazing hook...
@moylababa8196
@moylababa8196 3 года назад
please continue the graphql series
@PedroTechnologies
@PedroTechnologies 3 года назад
I will continue, I want to post some other stuff in between as well! Next gql episode is already recorded and will come out tomorrow!
@mohammadyaseenshaik4325
@mohammadyaseenshaik4325 Год назад
The api is having payloads then how to set in custom hook
@georgechong5065
@georgechong5065 Год назад
Is it possible to redirect to error page in usefetch hook???
@mwnkt
@mwnkt 3 года назад
I didn't know where to look for this, but how can I fetch more data whenever I'm close to the bottom of the page? ie whenever you scroll super fast to the bottom on RU-vid you can see more videos being loaded.
@khurshed780
@khurshed780 3 года назад
Would I request you to share a folder structure for e-commerce project with admin panel and frontend for nextjs
@PedroTechnologies
@PedroTechnologies 3 года назад
I have a video going over an advanced folder structure for a react app! I would follow something like that :)
@khurshed780
@khurshed780 3 года назад
@@PedroTechnologies thanks for quick reply, yes I have seen that I want to implement two theme Admin panel and frontend in same src folder
@MizManFryingP
@MizManFryingP 2 года назад
How would you handle a situation where you are constantly polling data? When I do that, the screen flashes for a second because for a split second there while the information hasn't arrived yet, the UI cannot be displayed.
@TheMeepPlay
@TheMeepPlay 2 года назад
Thanks!!!
@pikolq7665
@pikolq7665 3 года назад
we need next js full course
@TheWolverine1984
@TheWolverine1984 2 года назад
Hey, what plugins one needs to use to make VS code look like that? I mean themes/icons
@mohammadyaseenshaik4325
@mohammadyaseenshaik4325 Год назад
How to call that api again if the state is changed
@sanjeebgochhayat6911
@sanjeebgochhayat6911 2 года назад
is there a way to perform post request with this useFetch custom hook
@chiranjibikarki8049
@chiranjibikarki8049 2 года назад
Hi Pedro, just a small enquiry about reusing the useFetch custom hook as shown to fetch the data. Suppose if we have a page which has multiple components that needs data from different APIs and in this case can useFetch hook be reused by all components at a same time? Because useFetch can receive an single url at a time and has single loading and error states, how can this be possible? or if can be reused at a same time then copies of useFetch hook is created in the memory to serve the purpose?
@ApartTour
@ApartTour Год назад
yeah you can reuse it, just make sure to give the values that are returned from the hook different names so you don't have a conflict. You can do so like this: const { data: usersData, loading: usersLoading, error: usersError, refetch: usersRefetch } = useFetch(...) this way you're still destructuring the variables you get from the hook, but give them different names
@chiranjibikarki8049
@chiranjibikarki8049 Год назад
@@ApartTour Thanks for your information
@luismarquez6653
@luismarquez6653 2 года назад
awesome tutorial! How can I use it on a component?
@abdulrahmanmamdouh7488
@abdulrahmanmamdouh7488 Год назад
can we expand this to support different API calls like post, put, and delete? maybe make it useAPI ? I'm not sure because then we will need to call the hook using some conditions and that doesn't work with hooks
@micekandy8742
@micekandy8742 2 года назад
Could you explain to me what it means in the code question mark and dot, like here: joke?.setup
@PedroTechnologies
@PedroTechnologies 2 года назад
It basically means that if the joke variable is null (meaning that the data from the api hasn't arrived yet) you then don't access the setup field since it wont exist.
@micekandy8742
@micekandy8742 2 года назад
@@PedroTechnologies Thank you :)
@vskyper
@vskyper 3 года назад
useFetch function shouldn't be async? Or it doesn't matter?
@puneetsingh9678
@puneetsingh9678 3 года назад
It would be async if you want to use async await. Here .then().catch() is used that does not require function to be async
@mohamedyoussef8835
@mohamedyoussef8835 2 года назад
Awesome +++++++++++++++++++++++++++++
@Jawad_Siddiqui
@Jawad_Siddiqui 2 года назад
video starts at 3.57
@augischadiegils.5109
@augischadiegils.5109 Год назад
nicee
@e-genieclimatique
@e-genieclimatique 2 года назад
why those who make programming videos do not listen to their own video he would hear the deafening noise of their keyboard...there are keyboards with soft keys that are not noisy
@PedroTechnologies
@PedroTechnologies 2 года назад
My keyboard changed is different now, back then that was the only keyboard i could afford. I can't have a perfect setup since the beginning. Im glad that now im able to afford better setup!
@joematkin3540
@joematkin3540 2 года назад
You talk as if he owes you something. He owes you nothing.
@saber1in
@saber1in Год назад
post, put, delete?
@thijs9339
@thijs9339 3 года назад
Hi Pedro thanks for the tutorial! Just a little question what extension do you use for auto tabbing your code and automatically adding a ";"?
@PedroTechnologies
@PedroTechnologies 3 года назад
Hey, the extension is prettier!
@coskuncabuk3119
@coskuncabuk3119 2 года назад
@@PedroTechnologies Thanks for this excellent videos. Although I find your video very useful nad clever, the jokes that API link display are clearly offensive for gays, blacks and Jews. I would recommend you use different API resources for teaching coding.
@sinaukode
@sinaukode 3 года назад
how about post, or delete request??, do we need to create more hook???
@PedroTechnologies
@PedroTechnologies 3 года назад
You can make another function inside of the hook which takes in a body as an argument and use the body to make a post request. Same thing with the delete request
@qweqw1359
@qweqw1359 2 года назад
Noice
Далее
Custom Hooks in React (Design Patterns)
12:56
Просмотров 45 тыс.
The Story of React Query
8:55
Просмотров 106 тыс.
Why Signals Are Better Than React Hooks
16:30
Просмотров 477 тыс.
4 Ways To Fetch Data in React
31:17
Просмотров 56 тыс.
I Never Want To Fetch Data Any Other Way
7:19
Просмотров 70 тыс.
Create A Custom Hook in React | React Hooks Tutorial
15:55
Use Axios with React Hooks for Async-Await Requests
36:01