How to use memo in React to Optimize Renders

  Рет қаралды 65,712

Ben Awad

Ben Awad

Күн бұрын

Пікірлер: 150
@gonzalochristobal
@gonzalochristobal 5 жыл бұрын
awesome video ben! i'll be glad to see more videos on optimization and more advanced react concepts like this!
@PJArmy
@PJArmy 3 жыл бұрын
Great video! I was looking for complex situations and real life example and this was the only video that got close to it!
@viet.khoaiegg
@viet.khoaiegg 2 жыл бұрын
This video is actually the best on KZbin about how to use memo and even useCallback
@aleksandarbisevac3129
@aleksandarbisevac3129 2 жыл бұрын
Wow, great example. In 10 minutes, this is the best explanation of memoisation and optimization of the react functional component. I have much better picture of useCallback and useMemo, also how to avoid writing callbacks inside React.memo. KUDOS to you Sir.
@gadayash9
@gadayash9 2 жыл бұрын
Not easy to find beginner to moderate-ish vids on such topics. Might have to go through this a thousand times over a few months, but thanks man
@schnubor
@schnubor 4 жыл бұрын
Nice! Would love to see a follow video though: "Why not use memo for all react components" ?
@marcel5235
@marcel5235 4 жыл бұрын
the number of bugs you will create with this approach or time spent to actually make it work as intended + keep the app fast is something that makes you think twice about that
@maximiliaanvandijk6111
@maximiliaanvandijk6111 4 жыл бұрын
@@marcel5235 Solution.. stop using react
@codelightsparkles2403
@codelightsparkles2403 3 жыл бұрын
@@maximiliaanvandijk6111 Why ? React is the king of jobs right now, other frameworks are lucky to have 50% of jobs react has. If it was me , I would have a look at Svelte for my own projects but if you meed a job then there is nothing better than react
@maximiliaanvandijk6111
@maximiliaanvandijk6111 3 жыл бұрын
@@codelightsparkles2403 Then why are you using memo. Don't think I've seen the need for that in other frameworks. Quite sure jQuery had a high job availability as well ;)
@vkj3131
@vkj3131 3 жыл бұрын
@@marcel5235 to
@jayaprakashreddy560
@jayaprakashreddy560 5 жыл бұрын
Awesome!! straight to the point. liked the point that how to handle the ref change of non-simple types (objects, functions) ..
@firstnamelastname4685
@firstnamelastname4685 3 жыл бұрын
in 6:17 When App render , The ref to settext did change right? then how line 10 works
@PAD637
@PAD637 3 жыл бұрын
@9:29 couldn't you also use useRef for that data object which would also prevent re-creating of the object right?
@vibekdutta6539
@vibekdutta6539 3 жыл бұрын
Ben awesome video, I had a small doubt, when u clik the increment button the Count component re-renders the state changes, but after u have wrapped the onOdd() with useCallback(), everytime you type the setText changes and it should also change the onOdd() isnt it?
@maxaquilino7264
@maxaquilino7264 4 жыл бұрын
Hey Ben good work on this as usual. It'd be great to create a video on how to optimize performance when using Flatlist applying Memo and Callback. This video explains clearly how to prevent re-rendering on functions, what about other types of components like classes. Cheers!
@EagerEggplant
@EagerEggplant 3 жыл бұрын
Arent classes just objects in JS?
@mr-skorpion
@mr-skorpion 3 жыл бұрын
Awesome vid as always Ben and thanks for flexing those vim skills 😂
@malikbrahimi7504
@malikbrahimi7504 4 жыл бұрын
Why was your dependency setText for the callback? Couldn't you have left it empty? Also can you do a tutorial on using the profiler to detect optimization issues?
@scottmai7457
@scottmai7457 3 жыл бұрын
Amazingly clear and concise - thanks Ben!
@MohiyuddinShaikh
@MohiyuddinShaikh 3 жыл бұрын
Wonderful video!
@EVA_Hamza
@EVA_Hamza 4 жыл бұрын
Amazing, we need more videos like this
@avisterdeveloper1120
@avisterdeveloper1120 2 жыл бұрын
Thanks crack! the only video I got that concept
@WanderlustThamur
@WanderlustThamur 4 жыл бұрын
dude so simple and yet so clean, thank you very much!!!
@westernpigeon
@westernpigeon 2 жыл бұрын
5:35 what is a lambda? tried googling but all i get is AWS Lambda and im sure they're not related
@cutipets8706
@cutipets8706 3 жыл бұрын
so basically useMemo used to memorize objects and useCallBack with functions? is i'm right?
@ngocdangduc3772
@ngocdangduc3772 Жыл бұрын
I got question, Why this is not implemented by default in react ?
@chethanprabhu4475
@chethanprabhu4475 3 жыл бұрын
Please someone help me. What if the parent was class based component? How can we make sure child does not rerender when we pass an function as prop to child.?
@metin4yt
@metin4yt 4 жыл бұрын
What if I have some data that's coming from a Context. That context gets updated via a WebSocket. This causes me a render on which notification. Which I guess it's fine. But I have an for each item in data, and I would like to re-render only the new items, not the old ones aswell. How can that be managed?
@rubenmartinez2807
@rubenmartinez2807 3 жыл бұрын
With a memoized list item component
@md.siddiq7165
@md.siddiq7165 3 жыл бұрын
This is literally an awesome content on performance optimization in react. Thanks Ben ❤️❤️
@farizma
@farizma 4 жыл бұрын
Bro this is amazing, I've been struggling to understand where to use memo actually. U made it so clear ❤️❤️
@alimirazimzadeh544
@alimirazimzadeh544 3 жыл бұрын
best explanation ever in 3 minutes
@darryl_young
@darryl_young 5 жыл бұрын
Thanks for sharing, Ben.
@diegounanue
@diegounanue 4 жыл бұрын
I know that child components are updated when parent is updated. But if you don't have the ref that you use in the child component, when react compares the virtual dom with the dom, what part will be uptaded in the child component? If the virtual dom of the child is the same as the dom.
@arwysyah2400
@arwysyah2400 4 жыл бұрын
How to avoiding multiple rander if i am using flatlist in parents and passing props to Child component , while i am fetch data and assign to state,and pss props to child, it’s showing multiple rander in number of props that i passed,how to avoid that ?
@rorycawley
@rorycawley 5 жыл бұрын
Excellent video, brilliantly explained. Thanks for going to that trouble.
@carlosbenavides670
@carlosbenavides670 4 жыл бұрын
Really nice explanation. Thanks for sharing.
@surajitdas94
@surajitdas94 5 жыл бұрын
Great one! Really appreciate your effort. Thanks a lot.
@mohammedg485
@mohammedg485 2 жыл бұрын
thank you so much بن عوض
@abhaykumar9806
@abhaykumar9806 4 жыл бұрын
Please tell me how can I optimize redux action/state mapped as props in react components???
@marcel5235
@marcel5235 4 жыл бұрын
Using reselect library which memoizes created selector. That prevents to redundant render. Actions should be triggered once in ui and ideally have controll component and dumb component, ie if u have actions that fires in useEffect have them grouped
@elie2222
@elie2222 5 жыл бұрын
What are the best tools you’ve seen to check for components rendering too much when you do run into performance issues?
@bawad
@bawad 5 жыл бұрын
the react profiler is pretty nice
@patrickren7395
@patrickren7395 4 жыл бұрын
Can I thonk of useCallback as a special version of useMemo?
@starsky411
@starsky411 4 жыл бұрын
There is a small mistake, at 7:45, the component will NOT re-render if you return TRUE. its the opposite actually, it will re-render if you return FALSE.
@tonydanza4502
@tonydanza4502 5 жыл бұрын
is React.useCallback() necessary for all callbacks where you are trying to avoid unneccessary rerenders of a memo-ized component? Wonder if you set onOdd equal to a plain old function expression like const onOdd = () => {setText(' ') } and ? I'm wondering why useCallback was only introduced with hooks and not before, was this optimization not available pre-hooks?
@bawad
@bawad 5 жыл бұрын
const onOdd = () => {setText(' ') } still creates a function each time in classes you could create methods that weren't created on each render class MyComponent { onOdd = () => {} render() { return ( ) } }
@tonydanza4502
@tonydanza4502 5 жыл бұрын
Ben Awad gotcha, thanks so much!
@nicofraisse8380
@nicofraisse8380 4 жыл бұрын
What color scheme are you using? Looks really nice
@benjaminhobson2085
@benjaminhobson2085 3 жыл бұрын
Brilliant man. Thanks.
@hanggianggono3765
@hanggianggono3765 5 жыл бұрын
Thank you BEN AWAD
@Mr.QLead1417
@Mr.QLead1417 Жыл бұрын
react says that if you have "expensive component" or "massive rerenders in a second" then you should use "memo". If there isnt any lag or smth, they say it is OK. So, because of the old PureComponent approach, everybody puts memo to every functional component. Because they didnt want to rerender the functional component. But, if the component arent expensive and it is not rerendering simultaneously (for example onResize or coordinates changing events), then shouldn't we just leave the component without memo? There are a lot of discussion about this topic, and such this videos, new react developers may get confused or make misusages about memo. What do you think about ? I think a more important topic is deciding whether a component is expensive or not. When to use "memo" and do we need performance optimization here or not ?
@GoeHybrid
@GoeHybrid 5 жыл бұрын
This is awesome! Thank you!
@23sagunjaiswal23
@23sagunjaiswal23 4 жыл бұрын
best video found after 2 days of trying to understand this
@jordiyaputra8359
@jordiyaputra8359 3 жыл бұрын
You saved my code, thank you
@jooooo9572
@jooooo9572 3 жыл бұрын
thank you for great content
@tonydanza4502
@tonydanza4502 5 жыл бұрын
is there a good strategy for memoizing a component that is passed a prop that is a complex object, like an array of objects that you'd get back from an API?
@bawad
@bawad 5 жыл бұрын
Does the complex object change often?
@tonydanza4502
@tonydanza4502 5 жыл бұрын
Ben Awad no
@bawad
@bawad 5 жыл бұрын
then the reference to the object should stay the same and memoizing should work
@tonydanza4502
@tonydanza4502 5 жыл бұрын
@@bawad sorry I mispoke what was actually happening, though physically they look the same, the objects are definitely different. What I am doing is setting an interval every 10 seconds to get information back from an api and then rehydrating the state with what I get back. Is there a technique for reducing rerenders when a shallow comparison is not enough?
@tonydanza4502
@tonydanza4502 5 жыл бұрын
I guess I could handle this situation another way, when i setState every time that interval is executed I could check to see if the objects have changed, otherwise don't set state. I wonder if that's best practice?
@AnNguyen-uv9yc
@AnNguyen-uv9yc 2 жыл бұрын
amazing bro
@alanson76
@alanson76 3 жыл бұрын
Awesome, Thanks a lot!!!
@huynhut5395
@huynhut5395 5 жыл бұрын
I tried with string, number, boolean, array, object, function and result was: not-rerender: string, number, boolean rerender: array, object, function And i used useMemo with Array similar to Object but it's re-render? Can you help me!
@bawad
@bawad 5 жыл бұрын
the reference to the array, object, function is changing each render then
@naseer036
@naseer036 2 жыл бұрын
Very good content
@ChavitaRocha
@ChavitaRocha 5 жыл бұрын
Hi Ben! I was curios about What keyboard do you use? :P Thanks for your videos :)
@bawad
@bawad 5 жыл бұрын
das keyboard
@abhaykumar9806
@abhaykumar9806 4 жыл бұрын
Why is my renders.current value double the count in React.StrictMode?? Any idea....
@chass8862
@chass8862 4 жыл бұрын
i had the same issue
@chass8862
@chass8862 4 жыл бұрын
i believe strict mode is used to find side effects and in order to help you do this is invokes methods such as render twice rather than once like react would normally
@Chocoholic1337
@Chocoholic1337 5 жыл бұрын
Hey Ben! I've learned about a really wicked extension for VSCode - it's called IntelliCode. It adds suggested autocomplete based on "AI" (Basically they scanned a ton of repositories). So for instance: const string = "potato"; string. - will show .split, .replace, .toLowerCase. But if I do if (string.){} it will show .length, .indexOf, .,match etc. Look into it, I think it's cool!
@bawad
@bawad 5 жыл бұрын
cool, I'll check it out
@planetmall2
@planetmall2 5 жыл бұрын
Great example!
@Daniol
@Daniol 5 жыл бұрын
Can you say what's the name of this font?
@bawad
@bawad 5 жыл бұрын
dm
@nuiatsun
@nuiatsun 5 жыл бұрын
dank.sh/
@arturgaik4777
@arturgaik4777 4 жыл бұрын
thanks dude!
@muratasarslan2359
@muratasarslan2359 4 жыл бұрын
Wonderful !
@deshario
@deshario 3 жыл бұрын
Wanna some examples with graphql useQuery[apollo-client lib]
@hardikchawla4966
@hardikchawla4966 4 жыл бұрын
You are the best !!
@amershboul9107
@amershboul9107 3 жыл бұрын
very useful
@user-ke2on2ju8m
@user-ke2on2ju8m 4 жыл бұрын
thank u so much!
@Givemilesrewardmiles
@Givemilesrewardmiles 5 жыл бұрын
can you please do a similar video but without using hooks?
@elie2222
@elie2222 5 жыл бұрын
Miles Rostami look at React.purecomponent, add functions to the class and not in render, and look at shouldcomponentupdate to the same sort of thing with older versions of React using classes
@bawad
@bawad 5 жыл бұрын
kzbin.info/www/bejne/l6nToZ2ln9GIpNk
@uusa35
@uusa35 4 жыл бұрын
thank you
@modestas2890
@modestas2890 5 жыл бұрын
Are you using vim?
@bawad
@bawad 5 жыл бұрын
Yes
@D4MJ4NTTK
@D4MJ4NTTK 5 жыл бұрын
brilliant
@budiprakosa6984
@budiprakosa6984 5 жыл бұрын
thanks!
@miguel900030
@miguel900030 2 жыл бұрын
You literally saved my ass
@krillkusb5887
@krillkusb5887 3 жыл бұрын
helpful
@rtorcato
@rtorcato 5 жыл бұрын
Problem really is that people build massive components that do too much. In this demo it would be way easier to just build an input component that handles it's own state on change.
@bawad
@bawad 5 жыл бұрын
yes and no, sometimes you need to raise the state
@js_programmer8423
@js_programmer8423 2 жыл бұрын
I am so confused, it seems like React.memo, useMemo & useCallback all do the same exact thing, fml.
@TheSirmousavi
@TheSirmousavi 4 жыл бұрын
Arrrgh! God, the IDE error messages !!!
@rezan6971
@rezan6971 3 жыл бұрын
they said hooks to make it simple.. they lied
React Hooks Closure Problems
7:21
Ben Awad
Рет қаралды 25 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
Preventing React re-renders with composition
12:11
Developer Way
Рет қаралды 24 М.
Redux or Context in React
6:16
Ben Awad
Рет қаралды 100 М.
What is React.memo - It's not useMemo
15:29
Dave Gray
Рет қаралды 9 М.
You Probably Shouldn't Use React.memo()
10:17
Theo - t3․gg
Рет қаралды 59 М.
Learn useMemo In 10 Minutes
10:42
Web Dev Simplified
Рет қаралды 498 М.
React Hooks useCallback Tutorial
11:15
Ben Awad
Рет қаралды 115 М.
React Hooks useMemo Tutorial
11:21
Ben Awad
Рет қаралды 78 М.
You should use React.memo more!
12:07
Zen of Coding
Рет қаралды 10 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 112 М.
#trending #foryou #challenge #fyp #viral #short #tiktok #vs
0:15
Misiсatсh
Рет қаралды 2,4 МЛН
Заставила парня продать зажигалки
0:52
Жизнь Барахольщика
Рет қаралды 3,3 МЛН
Таким раствором работать одно удовольствие
1:00
Профессия созидатели
Рет қаралды 954 М.