Тёмный

Async React Hooks 

Harry Wolff
Подписаться 28 тыс.
Просмотров 51 тыс.
50% 1

Welcome to an exciting new installment of 'Harry Plays with React Hooks!'
Today's episode we're getting deep into how to write Async React Hooks!
We're going to use a fun real world example for this video - searching for gifs on Giphy.com!
Take a few moments out of your day to learn how to write Async React Hooks!
(We also write a Custom Async React Hook!)
Source code: github.com/hsw...
--
PATREON: / hswolff
TWITTER: / hswolff
SITE: hswolff.com/
NEWSLETTER: tinyletter.com...

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

 

7 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 124   
@synthapise
@synthapise 3 года назад
You have the ability to make things go into my brain really easily. Thank you sir.
@aaronmoore9936
@aaronmoore9936 5 лет назад
Great video! Props for not being scared to show your personality in these videos. It makes the content great.
@hswolff
@hswolff 5 лет назад
Eyyyyy, great to hear! Code is for humans, right?!
@brunormferreiraa
@brunormferreiraa 4 года назад
@@hswolff Hero :)
@richardbrown3639
@richardbrown3639 5 лет назад
@4:48 TENSE!!! SAY TENSE!!! 😂 I love your content Harry, very informative. Keep up the good work! 👏🏻
@hswolff
@hswolff 5 лет назад
Hahah TENSE!
@nson__
@nson__ 4 года назад
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!
@zlatkoiliev8927
@zlatkoiliev8927 3 года назад
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
@koundamanee
@koundamanee 4 года назад
Very simple and effective tutorial on async react and custom hooks! Thank you!
@hswolff
@hswolff 4 года назад
Thank you! And you are welcome!
@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!
@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.
@jdevcast6527
@jdevcast6527 4 года назад
I love the fun example. It really solidified the concepts. I subscribed without hesitation!
@Gordolone
@Gordolone 3 года назад
Recently I have been watching some of your react videos, your way of explaining things is really nice and comprehensive. Nice video
@ahlambeyoud1709
@ahlambeyoud1709 4 года назад
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 👍 👏 😁
@robertosoriano9617
@robertosoriano9617 5 лет назад
THANK YOU for this video!!! Definitely needed this. Just this weekend I was having issues with useEffect.
@hswolff
@hswolff 5 лет назад
Woohoo!!! Glad it helped!
@NeerajKumar-nr6bl
@NeerajKumar-nr6bl 4 года назад
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 года назад
You are very welcome!
@makeit_studio
@makeit_studio 3 года назад
Man, your way of teaching is dope! Thank you so much, from Russia with love :)
@hswolff
@hswolff 3 года назад
Woohoo! So glad to hear that!
@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!
@tannerbarcelos6880
@tannerbarcelos6880 3 года назад
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!
@diegor5805
@diegor5805 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 3 года назад
very advanced principles of react, but so interesting i have learned a lot a beginner thanks !
@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!
@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
@Siddharathbhardwaj
@Siddharathbhardwaj 4 года назад
I just learnt react then u did really good explanation about async hooks.
@hrmny_
@hrmny_ 5 лет назад
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 лет назад
It doesn’t prevent them by default, correct, but it does raise their visibility to aid when debugging.
@tak68tak
@tak68tak 4 года назад
Awesome again! There are so many amazing videos in your channel.
@hswolff
@hswolff 4 года назад
d'awww, thank you!
@akalrove4834
@akalrove4834 3 года назад
Love this. Subbed and liked.
@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. 👍
@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!
@granthawkins88
@granthawkins88 3 года назад
Great overview. Will def share w loved ones.
@ivicajelavic1612
@ivicajelavic1612 2 года назад
Great job.
@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!
@adolfalexandr-right5732
@adolfalexandr-right5732 4 года назад
Man u are really good at explainig something with some fun) All the best from Russia)
@hswolff
@hswolff 4 года назад
Hey thank you! Appreciate hearing that!
@edustreamimg
@edustreamimg 3 года назад
Hello Harry, have you some video with multiple async calls and useReducer?
@AniDormi
@AniDormi 3 года назад
what does the value={search} attribute on the input tag do?
@abreguabel
@abreguabel 4 года назад
So Clear ! Crystal ! Thanks a lot !
@naynyamish270
@naynyamish270 4 года назад
Would love to see a debounce or trottle feature on this async hooks search , great video nonetheless.
@devolee8302
@devolee8302 4 года назад
I wonder why he still has only 7.83k subscribers?? He should've been a RU-vid rock star by now?
@hswolff
@hswolff 4 года назад
Maaaaan I wonder the same thing. Where are all my peeps at?!
@mtcindianutube
@mtcindianutube 3 года назад
Thanks for this detailed video :)
@claudioseccia9380
@claudioseccia9380 4 года назад
Piece ok cake! You have some skills!!! Thanks Sir!
@hswolff
@hswolff 4 года назад
Hahaha, thank you!
@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.
@nove1398
@nove1398 5 лет назад
Appreciate this video, the async await adaption was a bit misunderstood by me.
@hswolff
@hswolff 5 лет назад
Woo! Glad it helped!
@vssvqwwp337
@vssvqwwp337 5 лет назад
Great video! Very informative and entertaining at the same time ^^
@hswolff
@hswolff 5 лет назад
Woohoo! The double whammy wins!
@SoftwareSolutons
@SoftwareSolutons 4 года назад
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?
@nana6352
@nana6352 3 года назад
On what model of iphone this video was made? looks great
@mjohnson510
@mjohnson510 5 лет назад
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 лет назад
Yeah didn’t want to go too far with that in this video but I’m so glad you commented about it!
@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
@dimaio1759
@dimaio1759 4 года назад
Suppose airplane gifs are for 'undefined' query string ;)
@vaibhavm1986
@vaibhavm1986 4 года назад
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
@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.
@sympijs
@sympijs 5 лет назад
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 4 года назад
Ah yeah probably. That'd be a nice little improvement.
@mohammaddarbandi6285
@mohammaddarbandi6285 3 года назад
I'm sorry that I did not get acquainted with your higher education earlier ❤❤❤
@Exotonic
@Exotonic 4 года назад
Great Video Harry! Greetings from germany :)
@hswolff
@hswolff 4 года назад
Thanks!
@loversareinsane
@loversareinsane 3 года назад
How do you test this?
@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
@brunormferreiraa
@brunormferreiraa 4 года назад
you are good man! very nice video, I liked to watch u :D
@hswolff
@hswolff 4 года назад
woo! thank you for watching!
@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?
@XinWongDigital
@XinWongDigital 3 года назад
Great video. Really enjoyed it. Wud b coolto add some unit testing.😂
@pradeep422
@pradeep422 4 года назад
hooks == blocs???( dart || flutter world) seem so similar when u think abstractly...
@mayurmahajan1503
@mayurmahajan1503 3 года назад
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.
@jonathanyngfors3905
@jonathanyngfors3905 5 лет назад
Great video! Thanks! 🙏🏼🙏🏼🙏🏼
@hswolff
@hswolff 5 лет назад
You are welcome!
@josepicci6044
@josepicci6044 4 года назад
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?
@diegorenteria3289
@diegorenteria3289 4 года назад
Amazing video!! Thank you.
@hswolff
@hswolff 4 года назад
You are welcome! Thanks for watching!
@joejazdzewski
@joejazdzewski 4 года назад
You need to have clean up in your useEffect. To ignore the data returned if the request was killed.
@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?
@adeshas83
@adeshas83 4 года назад
great
@wdevon99
@wdevon99 4 года назад
Great Video! Thank you :)
@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
@Damixx111
@Damixx111 5 лет назад
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 лет назад
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.
@planetmall2
@planetmall2 5 лет назад
Great video!
@hswolff
@hswolff 5 лет назад
Thank you!
@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 😎
@rakshithkumar1430
@rakshithkumar1430 4 года назад
Thank you so much
@hswolff
@hswolff 4 года назад
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 лет назад
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 3 года назад
Thanks for not making a counter!!! Any tips on organizing state when using redux?
@hamzakhattabi9251
@hamzakhattabi9251 4 года назад
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 4 года назад
Can just override eslint with `// eslint-disable`. Sometimes you know better than the linter!
@hamzakhattabi9251
@hamzakhattabi9251 4 года назад
@@hswolff It's a good practice to disable it ?
@kirillzaytsev8105
@kirillzaytsev8105 4 года назад
OG Hook ROFL
@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 4 года назад
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 4 года назад
@@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
@stephanoiucciolino8215
@stephanoiucciolino8215 4 года назад
BUENARDO
@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 лет назад
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.
@Qors1314
@Qors1314 4 года назад
ur dope af
@hswolff
@hswolff 4 года назад
no ur dope af!
@giacomogagliano5179
@giacomogagliano5179 3 года назад
Boss =)
@pencilcase6148
@pencilcase6148 3 года назад
hungry learner i am
@MaksymMinenko
@MaksymMinenko 3 года назад
Too much clowning...
@edmilinski1295
@edmilinski1295 4 года назад
you got another view, lasted only 5 seconds ...
@hswolff
@hswolff 4 года назад
Thanks!
@Daddytronics
@Daddytronics 3 года назад
Too much face
@hswolff
@hswolff 3 года назад
😭
Далее
Playing with Framer Motion
14:32
Просмотров 5 тыс.
Why I Love useReducer
22:40
Просмотров 92 тыс.
ПРОСТИ МЕНЯ, АСХАБ ТАМАЕВ
32:44
Просмотров 2,2 млн
Why React Hooks?
14:04
Просмотров 174 тыс.
HOC Pattern in React
20:51
Просмотров 8 тыс.
State Managers Are Making Your Code Worse In React
13:33
Is SolidJS Better Than React?
15:22
Просмотров 14 тыс.
Custom Hooks in React (Design Patterns)
12:56
Просмотров 43 тыс.
React Hooks: Advanced Hooks
20:52
Просмотров 87 тыс.
Fun with React Hooks - Michael Jackson and Ryan Florence
1:02:51
Pager Dots Animation with React Native (Reanimated)
29:33
Tips For Using Async/Await in JavaScript
16:26
Просмотров 395 тыс.
React Hooks useContext Tutorial (Storing a User)
11:38
Просмотров 358 тыс.