Async React Hooks

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 พ.ย. 2024

ความคิดเห็น • 126

  • @synthapise
    @synthapise 4 ปีที่แล้ว +2

    You have the ability to make things go into my brain really easily. Thank you sir.

  • @aaronmoore9936
    @aaronmoore9936 5 ปีที่แล้ว +2

    Great video! Props for not being scared to show your personality in these videos. It makes the content great.

    • @hswolff
      @hswolff  5 ปีที่แล้ว +3

      Eyyyyy, great to hear! Code is for humans, right?!

    • @brunormferreiraa
      @brunormferreiraa 5 ปีที่แล้ว

      @@hswolff Hero :)

  • @nilfux
    @nilfux 2 ปีที่แล้ว

    You can write a useCallback async method and call it directly from the useEffect. You don't need to embed it within the effect.

  • @zlatkoiliev8927
    @zlatkoiliev8927 3 ปีที่แล้ว +6

    Great video! Only one thing, avoid inline methods, this is recreating the function every time when DOM renders. Instead pass a reference to the function, as this is not recreating the function over and over.

    • @nilfux
      @nilfux 2 ปีที่แล้ว

      useCallback

  • @nson__
    @nson__ 4 ปีที่แล้ว +1

    I'm a PHP oriented dev who just started to learn React. Thank you, because of your videos it's pretty easy to grasp the concepts of React/JS!

  • @tareb_b
    @tareb_b 3 ปีที่แล้ว

    One point that I noticed, you missed to pass `query` to fetchData at 19:00.
    So `maybe` keyword were not working, probably returns first 10 gifs automatically.
    and thank you for this great video!

  • @tannerbarcelos6880
    @tannerbarcelos6880 4 ปีที่แล้ว

    I actually like this async hook rule. Makes the code cleaner. I ran into this problem recently in an app I’m making and I actually made an IIFE inside the hook to save some code space. Love me some react!

  • @andrathema4
    @andrathema4 4 ปีที่แล้ว

    Awesome!! :)
    After a few hours of trying to figure out why my state wasn't updating on time, I've found your video!
    Thank you so much!

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      That's tremendous to hear! Thank you for watching!

  • @jdevcast6527
    @jdevcast6527 4 ปีที่แล้ว

    I love the fun example. It really solidified the concepts. I subscribed without hesitation!

  • @granthawkins88
    @granthawkins88 3 ปีที่แล้ว

    Great overview. Will def share w loved ones.

  • @NeerajKumar-nr6bl
    @NeerajKumar-nr6bl 5 ปีที่แล้ว

    if I'll describe in words it'll take a lot but in short you help me to debug a really messy warning that I was getting from long time so thanks a lot..

    • @hswolff
      @hswolff  4 ปีที่แล้ว +1

      You are very welcome!

  • @diegor5805
    @diegor5805 4 ปีที่แล้ว +4

    For noobs like me: When naming a React custom hook, remember it must start with the word "use". Example: useAPI()

    • @bibob2010
      @bibob2010 4 ปีที่แล้ว

      Part 2 of that is to always check if what you need from the api call you make, example of that is 14:47 where he has 'item.images.preview.mp4'.

    • @mishasawangwan6652
      @mishasawangwan6652 3 ปีที่แล้ว

      no it doesn’t that’s a convention

  • @raabisliman
    @raabisliman 4 ปีที่แล้ว

    very advanced principles of react, but so interesting i have learned a lot a beginner thanks !

  • @koundamanee
    @koundamanee 4 ปีที่แล้ว +2

    Very simple and effective tutorial on async react and custom hooks! Thank you!

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Thank you! And you are welcome!

  • @hrmny_
    @hrmny_ 5 ปีที่แล้ว +1

    Not allowing async funtions in `useEffect` does not prevent race conditions, your code can very much have a race condition if you submit multiple times quickly, because the `setResults` function could be called at different times

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      It doesn’t prevent them by default, correct, but it does raise their visibility to aid when debugging.

  • @richardbrown3639
    @richardbrown3639 5 ปีที่แล้ว +6

    @4:48 TENSE!!! SAY TENSE!!! 😂 I love your content Harry, very informative. Keep up the good work! 👏🏻

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Hahah TENSE!

  • @makeit_studio
    @makeit_studio 4 ปีที่แล้ว

    Man, your way of teaching is dope! Thank you so much, from Russia with love :)

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Woohoo! So glad to hear that!

  • @abreguabel
    @abreguabel 4 ปีที่แล้ว

    So Clear ! Crystal ! Thanks a lot !

  • @dimaio1759
    @dimaio1759 4 ปีที่แล้ว

    Suppose airplane gifs are for 'undefined' query string ;)

  • @edustreamimg
    @edustreamimg 3 ปีที่แล้ว

    Hello Harry, have you some video with multiple async calls and useReducer?

  • @conw_y
    @conw_y 4 ปีที่แล้ว

    Thanks, great vid.
    One small thing, at 8:00, you have to be careful with that way of logging. In Chrome it will log whatever value that name points to when Chrome resolves the name, which isn’t necessarily the same value as it had at the time that your console.log call ran. (Sorry, bit tricky to explain.) It might be safer to use Object.assign or JSON.stringify in your console.log call.
    Thanks again for the vid. 👍

  • @yuichibencoolenhrs5527
    @yuichibencoolenhrs5527 4 ปีที่แล้ว

    Thank you for providing the async lecture. I actually tried the code by adding it to my app. It was fun, though, I need to get API key for GIPHY. It was worth it as I knew this website.
    From Kamakura, JAPAN

  • @robertosoriano9617
    @robertosoriano9617 5 ปีที่แล้ว +2

    THANK YOU for this video!!! Definitely needed this. Just this weekend I was having issues with useEffect.

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Woohoo!!! Glad it helped!

  • @armanrozika
    @armanrozika 4 ปีที่แล้ว

    Hi Harry, you fetch data when the state is changed, hence useEffect (similar to componentDidMount, perhaps). Can we just fetch the data when onSubmit called / search button clicked? so no need to use useEffect. I am having hard time to understand it because I rarely use componentDidMount. Or is it so we don't call fetchData in different places multiple times? with class component for example, if we need initial fetch we would do it in componentDidMount and later on in onSubmit, but with useEffect we can just use function fetchData once.
    Thanks for the tutorial, cheers!

  • @Gordolone
    @Gordolone 4 ปีที่แล้ว

    Recently I have been watching some of your react videos, your way of explaining things is really nice and comprehensive. Nice video

  • @akalrove4834
    @akalrove4834 3 ปีที่แล้ว

    Love this. Subbed and liked.

  • @ivicajelavic1612
    @ivicajelavic1612 3 ปีที่แล้ว

    Great job.

  • @ahlambeyoud1709
    @ahlambeyoud1709 4 ปีที่แล้ว +1

    thank you very much for this tutorial, it's really great to make examples that are very useful in creating real-world applications, keep up the great work you do 👍 👏 😁

  • @AniDormi
    @AniDormi 4 ปีที่แล้ว

    what does the value={search} attribute on the input tag do?

  • @orenka1991
    @orenka1991 3 ปีที่แล้ว

    Hello Harry! Thanks for the video really helpful.
    One note though which I found weird is how you could make the useEffect "think" that query is not a dependency anymore. And I realized that during the refactor you did not put the query as parameter to the refactored async function. I guess that was the reason you got a bunch of airplanes result for query "maybe". There was no query at all in the api call I guess. I don't know if anyone pointed this out before or if it is true at all :D would be interested in your opinion.
    I love your videos!! You explain things in very nice and understandable ways!

  • @mtcindianutube
    @mtcindianutube 3 ปีที่แล้ว

    Thanks for this detailed video :)

  • @davids-k429
    @davids-k429 2 ปีที่แล้ว

    19:00 "Not sure why there is a plane for maybe"
    It's because you moved the fetchData function outside the component and gave it a parameter for query, but you never passed in query. You were actually searching giphy for undefined and not for your searched term
    You didn't trick anything because you still needed to pass query into the fetchData function and React would have yelled at you for it not being a dependency
    Still a great video!

  • @nove1398
    @nove1398 5 ปีที่แล้ว

    Appreciate this video, the async await adaption was a bit misunderstood by me.

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Woo! Glad it helped!

  • @nana6352
    @nana6352 3 ปีที่แล้ว

    On what model of iphone this video was made? looks great

  • @Siddharathbhardwaj
    @Siddharathbhardwaj 4 ปีที่แล้ว

    I just learnt react then u did really good explanation about async hooks.

  • @devolee8302
    @devolee8302 5 ปีที่แล้ว

    I wonder why he still has only 7.83k subscribers?? He should've been a TH-cam rock star by now?

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Maaaaan I wonder the same thing. Where are all my peeps at?!

  • @naynyamish270
    @naynyamish270 4 ปีที่แล้ว

    Would love to see a debounce or trottle feature on this async hooks search , great video nonetheless.

  • @letletland2284
    @letletland2284 4 ปีที่แล้ว

    Hi harry, please try to make a video about react-query and how to use it along with usefffect hook, thx in advance

  • @mjohnson510
    @mjohnson510 5 ปีที่แล้ว +1

    I’ve with dealing with calling multiple API’s
    const [loading, SetLoading] = useState(false) sucks
    It’s better to just use:
    let mounted = false
    Then before calling the api:
    mounted = true
    Then after calling api:
    SetGiff(gif.data)
    mounted = false
    Works great when cancelled subscriptions

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Yeah didn’t want to go too far with that in this video but I’m so glad you commented about it!

  • @ObiWanKenobi_IceNation
    @ObiWanKenobi_IceNation 4 ปีที่แล้ว

    Hey, I think the fetchData doesn't really need a query parameter, it's already in scope from the useState hook 😉

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Yeah the example is a little contrived, for sure.

    • @ObiWanKenobi_IceNation
      @ObiWanKenobi_IceNation 4 ปีที่แล้ว

      @@hswolff a very good video nonetheless 😎

  • @tak68tak
    @tak68tak 5 ปีที่แล้ว

    Awesome again! There are so many amazing videos in your channel.

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      d'awww, thank you!

  • @claudioseccia9380
    @claudioseccia9380 5 ปีที่แล้ว

    Piece ok cake! You have some skills!!! Thanks Sir!

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Hahaha, thank you!

  • @sympijs
    @sympijs 5 ปีที่แล้ว +1

    I think that useGiphy hook misses a cleanup function where you set some flag that you no longer care about the response of the given query

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Ah yeah probably. That'd be a nice little improvement.

  • @joejazdz
    @joejazdz 5 ปีที่แล้ว

    You need to have clean up in your useEffect. To ignore the data returned if the request was killed.

  • @cptechno
    @cptechno 3 ปีที่แล้ว

    Great video! I'm and inventor and software developer as well. I create my own original software applications. My question to you is: How long can the async call wait for the response? In my situation I also write the service in the server. The maximum wait time should be configurable, but where would you put it?

  • @SoftwareSolutons
    @SoftwareSolutons 5 ปีที่แล้ว

    asyn function just help to avoid the callback sysntax ,.then(callback).then(callback2).
    How can we use callback with Picker componenet , if we need to use fetch method to get data for another Picker2 based on the selection of first Picker component?

  • @gustavocatalasverdrup
    @gustavocatalasverdrup 4 ปีที่แล้ว

    Nice video. I just don't quite understand why did you want to use the hook. You could just have a normal async function on submit. It would work anyway?

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Yes, it probably would have. It was more to show how you can do it.

  • @adolfalexandr-right5732
    @adolfalexandr-right5732 5 ปีที่แล้ว

    Man u are really good at explainig something with some fun) All the best from Russia)

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Hey thank you! Appreciate hearing that!

  • @Damixx111
    @Damixx111 5 ปีที่แล้ว +1

    Nice, thank you! Quick question: if we removed query/setquery and the useeffect call and just directly fetch onSubmit instead, would that lead to problems?

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Nope! That’s perfectly fine! I actually did that in version one of this talk and then realized i wasn’t showing async react hooks lol. So i did that for demo purposes.

  • @vaibhavm1986
    @vaibhavm1986 5 ปีที่แล้ว

    Hey Harry , I actually re watched your video today :) just to understand more about custom hooks ,Please correct me I am wrong I understood that with custom hooks difference between previous version's approach is that in hooks version we can return data from stateful custom hooks those can act like actual functional components, which we could not have been able to do in older versions , in your example we would have had to create a separate functional component that keeps on re rendering once we change query and pass results as props to that component , Please confirm also please make some more videos if possible that you can address use cases in which hooks based approach has more advantages with respect to class based components , one I already understood from your example

  • @rakshithkumar1430
    @rakshithkumar1430 5 ปีที่แล้ว

    Thank you so much

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      You are very welcome!

  • @volkaneligul
    @volkaneligul 5 ปีที่แล้ว

    Hey man, thanks your useful sharings.. Could you share us your development environmet that about vscode settings and your useful extensions for react (prettier, eslint etc...)? Thanks a lot again :)

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Ooh good idea. I’ve been asked this a couple of times so I’m gonna bump it near the top of my list of videos to make. Thanks for watching!

  • @ZeNz0r01
    @ZeNz0r01 4 ปีที่แล้ว

    Thanks for not making a counter!!! Any tips on organizing state when using redux?

  • @mayurmahajan1503
    @mayurmahajan1503 4 ปีที่แล้ว

    That's a great tutorial!! Questions, (1) you are using useState here, what would be a use useReducer instead? (2) if there are lots of updates but in the same component would you use lots of useEffects to update stuff? I feel we are using useEffects as a nice replacement of actions/redux.

  • @maxaquilino7264
    @maxaquilino7264 4 ปีที่แล้ว

    Hey Harry cool video, very useful as I've just moved to React Native.
    A quick questions around your example though using Async on a encapsulated function which is then called when needed...
    I'm adopting exactly the same concept in one of my component where I need to execute a series of API fetch calls (using Redux dispatch by the way) and then at the end move to a new screen where the data will be rendered. The problem I'm having is that if I use Async I may move to the new screen before all API calls have been retrieved resulting in an error in rendering therefore I'm forced to run all function calls in sequence (.then) and move to the new screen at the end. This is obviously proving to be rather slow as I'm calling about 10 different end points.
    Is it possible using what you showed in this video to run everything async and then the last action (move to the new screen) only after all API have been executed?
    Many thanks

  • @jonathanyngfors3905
    @jonathanyngfors3905 5 ปีที่แล้ว

    Great video! Thanks! 🙏🏼🙏🏼🙏🏼

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      You are welcome!

  • @josepicci6044
    @josepicci6044 5 ปีที่แล้ว

    Hello! love your videos, i have been trying to manage state with useReducer but im having trouble when fetching data from an api, could you demonstrate how to do this in a video? or should i be doing it with useState instead?

  • @loversareinsane
    @loversareinsane 4 ปีที่แล้ว

    How do you test this?

  • @wolfroad
    @wolfroad 5 ปีที่แล้ว

    Awesome Video. So lets say that you have an app with a number of async calls that use a idToken set in a global context. How would you organize those custom API calls. multiple files with multiple custom hooks?

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Could have an apiHooks.js file that exposes multiple custom hooks for each API call. Or just create one custom hook that lets you make API calls?

    • @wolfroad
      @wolfroad 5 ปีที่แล้ว

      @@hswolff thanks man. I went with one file exposing multiples. The problem I'm running into is that I am implementing this on a component that gets re-rendered multiple times (some for no good reason like states being updated that are not impacting the view) I feel like I have to memoize it

  • @planetmall2
    @planetmall2 5 ปีที่แล้ว

    Great video!

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Thank you!

  • @koekjan
    @koekjan 5 ปีที่แล้ว

    Great video! Very informative and entertaining at the same time ^^

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      Woohoo! The double whammy wins!

  • @mohammaddarbandi6285
    @mohammaddarbandi6285 3 ปีที่แล้ว

    I'm sorry that I did not get acquainted with your higher education earlier ❤❤❤

  • @XinWongDigital
    @XinWongDigital 4 ปีที่แล้ว +1

    Great video. Really enjoyed it. Wud b coolto add some unit testing.😂

  • @0xAlpine
    @0xAlpine 5 ปีที่แล้ว

    Hey , love your theme what's your setup?

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Theme is "Atom One Dark". Couldn't leave the Atom theme behind when I went to VSCode :)

  • @hamzakhattabi9251
    @hamzakhattabi9251 5 ปีที่แล้ว

    How te remove dependency warning if we need to run once useEffect? Because I have warning il all component with Empty Array passed in argument

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Can just override eslint with `// eslint-disable`. Sometimes you know better than the linter!

    • @hamzakhattabi9251
      @hamzakhattabi9251 5 ปีที่แล้ว

      @@hswolff It's a good practice to disable it ?

  • @Exotonic
    @Exotonic 4 ปีที่แล้ว

    Great Video Harry! Greetings from germany :)

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Thanks!

  • @adeshas83
    @adeshas83 4 ปีที่แล้ว

    great

  • @wdevon99
    @wdevon99 4 ปีที่แล้ว

    Great Video! Thank you :)

  • @pradeep422
    @pradeep422 4 ปีที่แล้ว

    hooks == blocs???( dart || flutter world) seem so similar when u think abstractly...

  • @Digibeario
    @Digibeario 5 ปีที่แล้ว

    Another great video, thanks for your effort... and honesty! :)

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Hehe, I think it makes the videos more fun when I’m scared stupid lol.

  • @diegorenteria3289
    @diegorenteria3289 5 ปีที่แล้ว

    Amazing video!! Thank you.

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      You are welcome! Thanks for watching!

  • @kirillzaytsev8105
    @kirillzaytsev8105 4 ปีที่แล้ว

    OG Hook ROFL

  • @brunormferreiraa
    @brunormferreiraa 5 ปีที่แล้ว

    you are good man!
    very nice video, I liked to watch u :D

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      woo! thank you for watching!

  • @babipal6315
    @babipal6315 5 ปีที่แล้ว

    Great video, thanks! Had a question - does having the fetchData function definition within the effect mean that function is getting created every time the effect is run? The function definition never changes, so it seems like a performance hit to keep redefining it.

    • @hswolff
      @hswolff  5 ปีที่แล้ว +1

      All those things are true!
      It gets created on every render and is a small performance hit!
      However the thing to consider is how much that (mostly undetectable) performance hit is worth versus the developer experience of having the eslint rule work correctly.
      Mostly I try and remember to not try and prematurely optimize my app until I need to. And I have still yet found a need to optimize the performance of any of my apps.

  • @whythisisntchanging
    @whythisisntchanging 4 ปีที่แล้ว

    search state is unnecessary - there's no need to get the input value from state when you can get it directly (with a ref, for example) from the input itself when form is submitted :)
    1. init the ref with const inputRef = useRef(null);
    2. use that ref on input -
    3. on form submit you can get the value of that input with inputRef.current.value

  • @stephanoiucciolino8215
    @stephanoiucciolino8215 4 ปีที่แล้ว

    BUENARDO

  • @Qors1314
    @Qors1314 4 ปีที่แล้ว

    ur dope af

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      no ur dope af!

  • @pencilcase6148
    @pencilcase6148 3 ปีที่แล้ว

    hungry learner i am

  • @giacomogagliano5179
    @giacomogagliano5179 3 ปีที่แล้ว

    Boss =)

  • @edmilinski1295
    @edmilinski1295 4 ปีที่แล้ว

    you got another view, lasted only 5 seconds ...

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Thanks!

  • @MaksymMinenko
    @MaksymMinenko 4 ปีที่แล้ว +1

    Too much clowning...

  • @Pigeon-envelope
    @Pigeon-envelope 3 ปีที่แล้ว

    Too much face

    • @hswolff
      @hswolff  3 ปีที่แล้ว

      😭

  • @RichardRamos-ro9qz
    @RichardRamos-ro9qz ปีที่แล้ว

    i don't understand how this work around 19:00
    fetchData().then(setResults);
    inside then, there is setResult.. but nothing set?