How to Fetch Data in React With A Custom useFetch Hook

Sdílet
Vložit
  • čas přidán 22. 08. 2021
  • 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/joke/Any
    Code: github.com/machadop1407/custo...
    🚀 Learn ReactJS By Building 6 Projects: codedamn.com/learn/reactjs-pr...
    Social
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    Website: machadopedro.com
    Linkedin: / machadop1407
    Instagram: / _pedro.machado_
    Github: github.com/machadop1407
    Email: machadop1407@gmail.com
    Tags:
    - ReactJS Tutorial
    - ReactJS and MySQL
    - NodeJS Tutorial
    - API Tutorial
  • Věda a technologie

Komentáře • 176

  • @0x0abb
    @0x0abb Před 2 lety +63

    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 Před 2 lety +2

      Well done sir

    • @mohshinmostafa3900
      @mohshinmostafa3900 Před rokem +1

      You are awesome too!

    • @saugatrajbhandari4247
      @saugatrajbhandari4247 Před rokem +2

      These type od comments motivates me

    • @aryankumar87771
      @aryankumar87771 Před rokem +1

      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 Před rokem +4

      @@aryankumar87771 Let other people do what they want to do

  • @_boza
    @_boza Před 2 lety +5

    You are one of the CZcamsrs who post real-life coding stuff that really helps! Thank you! :)

  • @ashishkumawat6110
    @ashishkumawat6110 Před rokem +2

    The best explanation of using a custom hook so far... Hats off man.

  • @Shaunmcdonogh-shaunsurfing

    The refetch was gold! Thank you man!

  • @capitaoisma
    @capitaoisma Před 2 lety +1

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

  • @digender
    @digender Před 2 lety +6

    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😃

  • @TheNamesJT
    @TheNamesJT Před 2 lety +5

    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.

  • @blockanese3225
    @blockanese3225 Před 2 lety +5

    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 Před 2 lety

      yet all these jobs want college graduates. this whole industry is honestly retarded these days.

    • @kevyyar
      @kevyyar Před 2 lety

      They tech react in college?

    • @blockanese3225
      @blockanese3225 Před 2 lety

      @@kevyyar yes and angular.

  • @michaelchinye7993
    @michaelchinye7993 Před 2 lety

    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

  • @wilber3015
    @wilber3015 Před rokem

    Wow! Thanks Pedro, by watching this video in just 16 min I was able to learn a lot about React & APIs

  • @dane-xmusic
    @dane-xmusic Před rokem

    Dude, Thanks much, been searching for this for hours!

  • @brandonp611
    @brandonp611 Před 2 lety

    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.

  • @bekzatmurat1341
    @bekzatmurat1341 Před rokem

    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.

  • @JoaoPiovensan
    @JoaoPiovensan Před 2 lety +1

    First video that i watch of this channel and already loved it, keep making the good work mate

  • @dezinhtang
    @dezinhtang Před 2 lety

    Great Explanation.I love the way you teach

  • @cassiosalvador7961
    @cassiosalvador7961 Před rokem

    Very good and concise explanation, thanks for that!

  • @minnuss
    @minnuss Před 2 lety

    Great video ! Clever and simple code, love it !

  • @AkashDas-vh4ru
    @AkashDas-vh4ru Před rokem

    Excellent brother! Keep it up.💙

  • @kakhabervk9405
    @kakhabervk9405 Před rokem

    Good explained use api call with custom hooks. Thank you!

  • @KC-hl4oj
    @KC-hl4oj Před měsícem

    Really great tutorial - thanks soooooo very much!

  • @ThColinPereira
    @ThColinPereira Před 2 lety

    Exactly what I was looking for!

  • @k303k
    @k303k Před rokem

    thanks a lot for the video pedro!

  • @nehanagda8888
    @nehanagda8888 Před rokem

    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.

  • @balgrimesart564
    @balgrimesart564 Před 2 lety

    really nice tutorial! following right now to see more of these 😀

  • @denniszheng7827
    @denniszheng7827 Před 2 lety

    Thanks for your course, it's helpful.

  • @exacode
    @exacode Před rokem

    Thanks man, this was very good

  • @_h4x0r
    @_h4x0r Před rokem

    Thank you, Pedro!

  • @cybertechzen5411
    @cybertechzen5411 Před 2 lety

    Tnx man it really helped keep it up

  • @d-landjs
    @d-landjs Před rokem

    Excellent info bro!!!

  • @marinasatsyk2184
    @marinasatsyk2184 Před 2 lety

    Tnak tou very mutch! Your explanations helped me a lot!

  • @milosnikolic4282
    @milosnikolic4282 Před 2 lety

    What a beutiful solution! Great video, it helped alot!

  • @JuliaMaschion
    @JuliaMaschion Před rokem +1

    I loved your channel! Already subscribed !

  • @GabrielSouza-sl8vp
    @GabrielSouza-sl8vp Před 2 lety +5

    Great vídeo again!
    It would be interesting to bring some videos of React using Typescript too.

  • @bear-code
    @bear-code Před 2 lety +1

    thank you man , you are legend

  • @nauni07
    @nauni07 Před 2 lety

    Awesome explanation

  • @shashankshekharsingh6076

    Simply lucid 👍🙏🏻

  • @Markdim7
    @Markdim7 Před 2 lety +1

    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  Před 2 lety

      Using a premade hook will improve the code quality! Good luck in the project :)

  • @borrokatu3656
    @borrokatu3656 Před 2 lety

    you explains conceps very well

  • @franco.delcas
    @franco.delcas Před 2 lety +1

    Cool stuff! thank u

  • @usmanirevo895
    @usmanirevo895 Před 2 lety +1

    Awesome video, well explained

  • @sonamohialdin3376
    @sonamohialdin3376 Před 2 lety

    So good tutorial very useful

  • @EK-rp8jp
    @EK-rp8jp Před 2 lety

    super useful! thanks a lot:>

  • @zeddscarlxrd4331
    @zeddscarlxrd4331 Před rokem

    Awesome dude thankssss

  • @progremeramatir
    @progremeramatir Před 2 lety

    Very good tutorial

  • @Pareshbpatel
    @Pareshbpatel Před 2 lety +1

    Excellent tutorial on fetching data using a custom react hook. Thanks, Pedro
    {2022-02-14} , {2022-02-18}, {2023-08-06}

  • @anbhuonline
    @anbhuonline Před 2 lety

    Simple and easy 👍

  • @FadlySansan
    @FadlySansan Před 2 lety

    thank you so much. I think I can move on from class & redux using this to make action and handle my state

  • @bastienv6233
    @bastienv6233 Před rokem

    Thank again for your Nice content

  • @khurshed780
    @khurshed780 Před 2 lety +1

    Great video plz keep it up 🚀

  • @jobhatti
    @jobhatti Před 2 lety +1

    Awesome stuff keep it up!

  • @agustinperez8700
    @agustinperez8700 Před 2 lety

    Nice video brou, I'm going to make my own implementation in typescript for my app

  • @Dipenparmar12
    @Dipenparmar12 Před 2 lety

    Amazing hook...

  • @freemenclub
    @freemenclub Před 2 lety

    Great, thanks!

  • @Xtopherization
    @Xtopherization Před 2 lety

    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 ?

  • @Lunolux
    @Lunolux Před 2 lety

    Great video

  • @abhaytiwari6411
    @abhaytiwari6411 Před 2 lety +1

    Splendid video 👍

  • @abdoulayendiaye1656
    @abdoulayendiaye1656 Před 2 lety

    YOU ARE AMAZINGGGGGGGGGGG

  • @mahmoudk528
    @mahmoudk528 Před 2 lety

    you are amazing!

  • @abhishekmishra9841
    @abhishekmishra9841 Před 2 lety +1

    Your tips helpful for us thank you brother more like this ❤️🙏

  • @diego0ji
    @diego0ji Před 2 lety

    Superr

  • @devinosborne3396
    @devinosborne3396 Před 2 lety

    Great video! How would you recommend making multiple calls in the same component?

  • @hamdskid
    @hamdskid Před rokem

    U saved my life 😂😂

  • @S--xc4rv
    @S--xc4rv Před 9 měsíci

    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

  • @TheMeepPlay
    @TheMeepPlay Před 2 lety

    Thanks!!!

  • @brenongr
    @brenongr Před 2 lety

    Valeu, man!

  • @TheWolverine1984
    @TheWolverine1984 Před 2 lety

    Hey, what plugins one needs to use to make VS code look like that? I mean themes/icons

  • @piotrszczesniak685
    @piotrszczesniak685 Před 2 lety

    awesome tutorial. the only this I didn't get is the notation { date?.setup }

  • @JasonWee
    @JasonWee Před 2 lety

    ROFLMAO... the api jokes is SOOOOOOOOOOOOOOOOOOOOOOOOO FUNNY

  • @evanvana1485
    @evanvana1485 Před 2 lety +3

    Great video! What theme are you using in VS Code?

  • @MizManFryingP
    @MizManFryingP Před 2 lety

    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.

  • @augischadiegils.5109
    @augischadiegils.5109 Před rokem

    nicee

  • @sanuyadav-ys3fb
    @sanuyadav-ys3fb Před rokem

    Thank you so much bro. Pleas econtinue to make such video

  • @luismarquez6653
    @luismarquez6653 Před 2 lety

    awesome tutorial! How can I use it on a component?

  • @chiranjibikarki8049
    @chiranjibikarki8049 Před 2 lety +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 Před rokem

      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 Před rokem

      @@ApartTour Thanks for your information

  • @subhashgn1775
    @subhashgn1775 Před 2 lety +1

    Very Well explained.👌
    Thank you🤝
    How to handle multiple API calls in single component?

    • @devinosborne3396
      @devinosborne3396 Před 2 lety

      I also have this question. I believe the way you can is by simply naming the arguments differently. Which is messy, but still possible.:/

  • @mwnkt
    @mwnkt Před 2 lety

    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 CZcams you can see more videos being loaded.

  • @pikolq7665
    @pikolq7665 Před 2 lety

    we need next js full course

  • @Mercio2
    @Mercio2 Před 2 lety

    nice

  • @TheNamesJT
    @TheNamesJT Před 2 lety +1

    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?

  • @johannsebastianbach3411

    In fact this is how react-query was invented by Tanner Linsley!

  • @yusufahmed3678
    @yusufahmed3678 Před rokem

    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.

  • @Dev-Phantom
    @Dev-Phantom Před měsícem +1

    cool

  • @thijs9339
    @thijs9339 Před 2 lety

    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  Před 2 lety

      Hey, the extension is prettier!

    • @coskuncabuk3119
      @coskuncabuk3119 Před 2 lety

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

  • @brunomorais4129
    @brunomorais4129 Před rokem

    how can I make the hook more dynamic by receiving the method by parameter?

  • @qweqw1359
    @qweqw1359 Před rokem

    Noice

  • @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

  • @divagesh
    @divagesh Před rokem

    what if we want to call multiple url /api in a component ?

  • @princenarteh8822
    @princenarteh8822 Před 2 lety +2

    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  Před 2 lety +1

      You can do the same thing, but also put an argument to the hook that represents the body of the post request :)

  • @eliasantagiuliana6379
    @eliasantagiuliana6379 Před 2 lety

    I have a double null response before the correct one if for example I console.log the data

  • @mohamedyoussef8835
    @mohamedyoussef8835 Před 2 lety

    Awesome +++++++++++++++++++++++++++++

  • @marcossequeira5433
    @marcossequeira5433 Před rokem

    its not much easy to instance the setLoading in true in the useState instead inside the useEffect?

  • @sanjeebgochhayat6911
    @sanjeebgochhayat6911 Před rokem

    is there a way to perform post request with this useFetch custom hook

  • @georgechong5065
    @georgechong5065 Před rokem

    Is it possible to redirect to error page in usefetch hook???

  • @moylababa8196
    @moylababa8196 Před 2 lety +1

    please continue the graphql series

    • @PedroTechnologies
      @PedroTechnologies  Před 2 lety +1

      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!

  • @elinev7830
    @elinev7830 Před 2 lety +1

    Could U explain how to do post or delete call, in the same custom hook?

    • @havelboumbidi440
      @havelboumbidi440 Před 2 lety

      I have been searching for the same thing. have you found anything? if yes could u share please?

  • @alphaknite1458
    @alphaknite1458 Před 3 dny

    i keep getting this error: Error: (0 , _Hooks_useFetch__WEBPACK_IMPORTED_MODULE_2__.default) is not a function
    im using typescript, I searched everywhere but cant find a solution, even chatGPT aint helping

  • @sumitdhakal6405
    @sumitdhakal6405 Před rokem

    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.

  • @JaredFL
    @JaredFL Před 2 lety

    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.

  • @aghilannathan8169
    @aghilannathan8169 Před 2 lety

    What is the reason to do this instead of using something like React Query?

  • @mohammadyaseenshaik4325

    The api is having payloads then how to set in custom hook