Тёмный

Common React Mistakes: useEffect - Part 1 

Jack Herrington
Подписаться 187 тыс.
Просмотров 46 тыс.
50% 1

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

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 78   
@xq_nemesis
@xq_nemesis 3 года назад
I’ve read some articles about using useCallback and useMemo on non expensive functions only decreases performance because you load more into memory then actually needed. I.e. not “optimizing” would have led to better performance. Do you agree with this?
@jherr
@jherr 3 года назад
Yes, I mentioned in the video that useCallback and useMemo weren't necessary in this particular example. I used them here because I'm teaching them and I didn't want a massive unwieldy example. IMHO, the perf penalty on useCallback and useMemo isn't huge, but... I would only use then if I'm writing a custom hook, or I'm passing their results down to a tree of components where I don't know how they will be consumed, and that's because of referential integrity. Ken C. Dobbs has a great article on this and the conclusion is that it's primarily about referential integrity.
@xq_nemesis
@xq_nemesis 3 года назад
@@jherr Thanks for the feedback! I find it very hard to know when or when not to use these kinds of optimizations as a beginner. Definitely going to look up that article.
@jherr
@jherr 3 года назад
@@xq_nemesis For beginners I would start by always using them. That will give you some serious experience with hooks and make the transition into building your own custom hooks a lot easier. You can always pull them out for perf reasons. Start with the idea that everything from the top of the function to the return should be in a hook, and then come back from that if you get pushback.
@mattmarkus4868
@mattmarkus4868 3 года назад
@@jherr Sorry to butt in here but when you say, "start with the idea that everything from the top of the function to the return should be in a hook", what are you referring to? I ask because I'm dying to take my mess of code and abstract the logic from the presentation. I just don't have the correct mental model for how to approach custom hooks at this point. Your channel is the best on RU-vid btw and I'm not just saying that!
@jherr
@jherr 3 года назад
@@mattmarkus4868 Thank you! Feel free to jump on the Discord server and we can talk through any specific example you might have. As with all this stuff it's pretty subjective. If you are doing the "hooks as a store" thing then yeah, I'd pull that out into a custom hook. If the code is very specific to this one component, and would never be reused, then I might think about leaving it be, unless it's too big, and then possibly refactoring it into a custom hook in the same file. One of the reasons I think doing more custom hooks is important is because you start learning the rules of dependency arrays and referential integrity and getting a good feel for when to use useCallback and useMemo.
@ramoncardenas8433
@ramoncardenas8433 2 года назад
I run into these problems every time I'm working on a react project. This one video is probably gonna save me a ton of time moving forward. Thank you!
@BezzantSam
@BezzantSam 2 года назад
Was surprised by the colors and background of this episode but I like it.
@Motivado-gp
@Motivado-gp 2 года назад
Hey, im from Brazil. Thanks for making these videos. I improve my code in React significantly every time i watch your tips. Thanks a lot ! Great work !
@amoljoshi23
@amoljoshi23 3 года назад
Awesome content Jack! So much learning condensed to 15-20 minutes. I would love to watch a video on some complex use cases of `useEffect`. For eg. 1. calling an API on on initial render -> saving that in state -> based on some interaction call the API again -> update state. 2. how many `useEffects` are too many? 3. Shortcomings of `useEffect` (where you need to end up using additional libraries like `react-use`). Thank you again for making fun videos
@jherr
@jherr 3 года назад
Great suggestion!
@matthewlefevre5667
@matthewlefevre5667 3 года назад
Thanks Jack! I don't ever use useCallback or useMemo. This video helped me to understand them better. As far as rules go: I tend to always pass a function when setting the value of useState just feels right for me.
@jherr
@jherr 3 года назад
It's certainly the safest way to avoid any staleness. That being said, sometimes it can make the code a little klunkier and harder to follow.
@Niamudeen
@Niamudeen Год назад
I'm a React Js developer but i have l learnt so many new things. Thanks Jack. ✌
@julian_pp
@julian_pp 2 года назад
Thanks you Jack, I would like to have a teacher like you!!! Your are a great young Master!!!
@mehdiwadoud8098
@mehdiwadoud8098 3 года назад
you provide one of the greatest programming content, Thank you so much
@HadeFX
@HadeFX 3 года назад
Hey Jack, recently discovered your channel. I have started a new front end project involving TS, React, Chakra UI and MobX. The content you're making at the moment is so relevant to me!!! Finding it really helpful. Keep up the good stuff, thanks
@sheneshperera
@sheneshperera 3 года назад
Hey Jack, awesome vid as always! Could you perhaps raise the zoom level of the code so it's a little easier to read on touch devices? Would be great if you could :)
@jherr
@jherr 3 года назад
I'll think about that. I've never really liked the Egghead thing of those massive fonts.
@BezzantSam
@BezzantSam 2 года назад
When I get discouraged about my skills learning React I can think back to learning guitar and bass. My fingertips hurt, I had to practice a lot for very little result. Eventually with a lot of hard work I was able to start playing some of the riffs and solos I so admired.
@shubhamh7451
@shubhamh7451 2 года назад
This is a perfect sum up video, thank you!
@zkayable
@zkayable 3 года назад
Real good content, as always. I wish it was with typescript though
@hamidrezakazemi7746
@hamidrezakazemi7746 3 года назад
what an awesome tutorial. Thanks
@renanfilipe10
@renanfilipe10 3 года назад
Thanks for the video! How about a video showing how to use the react profiler chrome extension? In this video, you can show how useCallback and React.Memo are used to avoid unnecessarily renders
@jamesbest2221
@jamesbest2221 3 года назад
Wow! So good! Thank you.
@junzhongloo2170
@junzhongloo2170 3 года назад
Your videos are truly amazing dude. Keep it up !!!
@sahilvig22
@sahilvig22 2 года назад
Thanks for the video. I loved it
@jasonadams5151
@jasonadams5151 3 года назад
Nicely done! I was admittedly hoping you were going to touch on the pitfall of using objects and functions as dependencies. Many people run into that not realizing that objects change even if they are equivocally the same.
@jherr
@jherr 3 года назад
I'll get there, I had to lay the groundwork first. I had enough people coming to me with the basics stuff that I had to cover that first and I didn't want to overwhelm with a ton on content.
@dabears51979
@dabears51979 2 года назад
@@jherr Hi Jack, have you created a video that addresses using objects as dependencies since this video?
@PfalzerGbr
@PfalzerGbr 3 года назад
Great video, it put these hooks in a little bit different perspective. It was nice starting from a broken app, and then break it again. Good to see the moving parts. Subscribe and bell button pressed :D
@ibrahimkhurshid4339
@ibrahimkhurshid4339 2 года назад
Besides everything, I love his eyes.
@subhashgn1775
@subhashgn1775 2 года назад
Very grateful to you. It is really helping me to master the react.
@petronini
@petronini 3 года назад
Common React Mistakes: useEffect → Jack Herrington useEffect 7:09 useCallback 12:48 useMemo 16:49
@jherr
@jherr 3 года назад
Thanks Jorge! Forgot about that.
@vnm_8945
@vnm_8945 3 года назад
I really like how you explain, you are great, keep up with the good work! Thank you for doing this! :)
@mmmmmmm8706
@mmmmmmm8706 3 года назад
Much appreciated.
@thanhnguyenhoai2727
@thanhnguyenhoai2727 3 года назад
Thank you sir Jack
@Ayomikun
@Ayomikun 3 года назад
Great video, just a heads up, you might want to increase the bit rate of the video, or maybe decrease the resolution of your face cam when showing the parts in VS code. That should fix the weird blocky background effect showing on your VS code background!
@jherr
@jherr 3 года назад
Will do, thanks!
@Ayomikun
@Ayomikun 3 года назад
@@jherr No worries, just hope it actually works!
@JulioDx3480
@JulioDx3480 3 года назад
This was a great video, thanks!
@d0paminer
@d0paminer 2 года назад
thx from Russia..!
@NuMarK35
@NuMarK35 2 года назад
Hi Jack, great video thanks and greetings from Buenos Aires. Should I use useMemo and useCallback whenever I can? In which cases I wouldn't have to use them? Thanks a lot !
@jherr
@jherr 2 года назад
IMHO, if you are building custom hooks you should use them. You should also use them if the function, or the data, that's being created, is passed to another component or being used in a dependency array. It's all about maintaining referential identity. When the data changes, the reference should change, when the data does not change, the reference should not change. Also, references only matter for arrays, objects, or functions. So if you are using useMemo to create a primitive type (string, number, boolean) then you should only use that if computing the value is time intensive.
@guilhermegirardi
@guilhermegirardi 2 года назад
Awesome!
@moarte6
@moarte6 3 года назад
Hi Jack. Would you consider doing a tutorial on testing? Maybe talk about TDD and the best way to test hooks, maybe testing for Redux as well? Feels like most RU-vidrs and content creators shy away from this topic which is unfortunate because it's so important.
@jherr
@jherr 3 года назад
I agree and yep, I'll add it to the list. Sadly though, it's a long list. :( You can emphasize it by joining the Discord server and putting a request in #video-ideas.
@r-i-ch
@r-i-ch 3 года назад
I'll call you out on a common Mistake 😉: No .catch() clause error handling for the fetch!! I mean at least tack-on .catch(err => console.error(err)); 😇 Good stuff as always...
@r-i-ch
@r-i-ch 3 года назад
Addendum thought of only-my-preference 'not a big deal', I think it is more predictable to define a named function for doing the fetching within the useEffect, and then call that function by name. Also let's you comment it out when you're working on other things...
@jherr
@jherr 3 года назад
You got me! ;)
@jherr
@jherr 3 года назад
That's an excellent piece of advice!
@attamjotsingh4474
@attamjotsingh4474 2 года назад
Awesome ✌️✌️
@mateusznowak8542
@mateusznowak8542 2 года назад
Thanks :)
@marouaniAymen
@marouaniAymen 3 года назад
Thanks for the video, but I still use the hook useCallback when my function is used inside a dependency array of a useEffect hook, other than this, I don't see a big gain in using useCallback to always wrap my functions.
@jherr
@jherr 3 года назад
In this case it’s not necessary that’s true. But in cases where you pass a function as a prop or return a function from a custom hook it’s good to useCallbavk.
@2penry2
@2penry2 3 года назад
Do you have a background in education? Because you are a seriously good teacher!
@jherr
@jherr 3 года назад
No, just another dev. :)
@2206901
@2206901 3 года назад
I use a flag too within useEffect, didn't know I was doing good practises =o
@jherr
@jherr 3 года назад
Yeah, if you need mounted state in another `useEffect` in the same area then you could do an `isMountedRef` with `useRef` where you have a `useEffect(() => { isMountedRef.current = true; return () => { isMountedRef.current = false; }}, []);` and use that to feed other `useEffects` so they don't have to track it as well.
@jornejongsma
@jornejongsma 3 года назад
Well, the rules that I try to live by... That's a good question, specially when it comes to refactoring components into smaller components. When and what to refactor?! Like with this component, you could start to create a button component for AddOne. But what do we give it, do we only give it the setter (setNumbers), so it can only function as a addOne button, or do we give it a onClick prop, so it can handle any kind of click event? But that wo don't refactor the evenhandler out of this component... And when is it time to introduce a state manager?
@petarkolev6928
@petarkolev6928 3 года назад
Rule #5 contradicts to the "useEffect" implementation here. I heard once Dan Abramov talking about how we shouldn't omit anything in our "useEffects"'s dependency array but I don't really see how this can be done every time :/
@ignaciokornzaft7062
@ignaciokornzaft7062 3 года назад
Hi Jack! Thank you for another great video. Just one question: how can we get rid of the annoying "missing dependencies" error that shows on the console at 14:20? I get those all the time!
@jherr
@jherr 3 года назад
Well, the fix there was to add `numbers` as a dependency or use the `setNumbers(oldNumbers => [...newNumbers])` state setter variant. I generally find that when this warning fires, it's for a good reason.
@ignaciokornzaft7062
@ignaciokornzaft7062 3 года назад
@@jherr Thanks for your answer!
@aapiner
@aapiner 2 года назад
Great !!
@ashimov1970
@ashimov1970 2 года назад
How come console.log("request finished") ever works? It should not since setNumbers(data) makes MyComponent re-render hence being trapped into an infinite loop that does not scope that console logging
@FranMcOd
@FranMcOd 2 года назад
Maybe add a note saying that this is not valid for react 18 anymore
@jherr
@jherr 2 года назад
Most of this video is about issues with the dependency arrays. And that doesn't change with React 18. The only thing that changes is the mount-unmount-remount behavior in development mode with strict mode enabled.
@markclynch
@markclynch 3 года назад
Rules to live by: never pre optimize.
@manansoni815
@manansoni815 3 года назад
Can you share font and theme?
@jherr
@jherr 3 года назад
Monalisa is the font, and I run a hacked version of Apollo Midnight. The hacking is to bring in the italics.
@dineshreddy8915
@dineshreddy8915 Год назад
what theam u r using
@jherr
@jherr Год назад
Night Wolf [black]
@aniketbhalla1521
@aniketbhalla1521 2 года назад
740 Likes with me..!
@joostdeveloper2192
@joostdeveloper2192 2 года назад
Jack could also be an auctioneer. So fast! Calm down please. Info is quite useful but it's not very easy to follow.
Далее
Common React Mistakes: useEffect - Part 2
20:44
Просмотров 26 тыс.
3 React Mistakes, 1 App Killer
14:00
Просмотров 116 тыс.
Гаджет из даркнета 📦
00:45
Просмотров 266 тыс.
На самом деле, все не просто 😂
00:45
#5: React Hooks - useCallback
23:54
Просмотров 51 тыс.
UseState: Asynchronous or what?
17:00
Просмотров 66 тыс.
Goodbye, useEffect - David Khourshid
29:59
Просмотров 501 тыс.
In Defense Of useEffect - React's Most Dangerous Hook
23:35
You're Doing React Hooks Wrong, Probably
20:11
Просмотров 64 тыс.
You Are Using useEffect Wrong
14:40
Просмотров 35 тыс.
Why Signals Are Better Than React Hooks
16:30
Просмотров 483 тыс.
Mastering React's useEffect
25:20
Просмотров 175 тыс.