Why do We Need Keys in React?

  Рет қаралды 10,845

Mehul - Codedamn

Mehul - Codedamn

Күн бұрын

In this video, I talk about the importance of keys in React and how virtual DOM enables React to only update the elements that have been changed.
⚛️ Learn Full Stack Development with React JS: cdm.sh/fullstack
Timestamps
0:00 Video Starts
0:31 Difference between DOM and Virtual DOM in React
1:27 Graph Representation of DOM
2:16 Graph Representation of Virtual DOM
2:42 Graph Representation of Map method
3:37 Working of Virtual DOM using keys
6:11 Common mistake while working with keys
7:33 Outro
👉 Free HTML5 + CSS3 Course: cdm.sh/htmlcsscourse
👉 Free JavaScript Course: cdm.sh/jsbasicscourse
👉 Free ReactJS Course: cdm.sh/react
👉 Free NextS Course: cdm.sh/nextjsbasicscourse
💰 Refer codedamn to friends and earn $$: cdm.sh/refer
👉 Checkout codedamn on social:
Instagram: cdm.sh/ig
Twitter: cdm.sh/twt
Facebook: cdm.sh/fb
GitHub: cdm.sh/github
If you found the video valuable, please leave a like and subscribe ❤️ It helps the channel grow and helps me pumping out more such content.
👋 About Me:
Hey! I'm Mehul Mohan, a CSE'21 graduate from BITS Pilani, Goa. I started coding early in life, at the age of 13 when I created my first blog on Blogger. I am listed in Google, Microsoft, Sony, eSet, etc. Hall of Fame for reporting vulnerabilities in their systems.
🍎 I am Apple's WWDC'19 Scholar - visited San Francisco and attended WWDC.
📙 I am an author of 2 books, on JavaScript and React - cdm.sh/mehulbooks
🚀 Currently working on my own startup - codedamn.
Connect with me:
📸 Instagram: cdm.sh/mehulig
📱 Twitter: cdm.sh/mehultwitter
📝 LinkedIn: cdm.sh/mehullinkedin
📂 GitHub: cdm.sh/mehulgithub
🌎 Personal website: cdm.sh/mehulblog
💻 Start learning to code for free: cdm.sh/codedamn
🧑‍🤝‍🧑 Discord community: cdm.sh/discord
👉 HTML5 + CSS3 Tutorials: cdm.sh/htmlcssytplaylist
👉 Complete JavaScript Tutorial: cdm.sh/jstutorial
👉 Complete React Crash Course: cdm.sh/reacttutorial
👉 Next JS Tutorials: cdm.sh/nextjsytplaylist
🏷 Video Tags:
Why do We Need Keys in React
React.js course
React 18 full course
Learn react.js free
React free tutorials
React course
React beginner tutorial
React.js full course
👉 Check out my other personal KZfaq channel (it is in Hindi/English mix) where I share coding tips, tricks, and hacks: cdm.sh/mehulyt

Пікірлер: 67
@codedamn
@codedamn 2 жыл бұрын
The example in video oversimplifies a few things. Think of key prop as linked to resetting the state of the component than a definitive flag for rendering. New rendering will always happen (until you memoize the component), however if your key remains same the internal component state will also remain same. Learn ReactJS along with several other Full Stack Development technologies on codedamn: cdm.sh/fullstack
@AdityaSharan811
@AdityaSharan811 2 жыл бұрын
The new method of teaching is amazing. It would be better if you do more videos like this.👍
@shashikantkalshetty
@shashikantkalshetty 2 жыл бұрын
Same kind of videos needed for topics like batching and async nature of setState()
@ppppp524
@ppppp524 2 жыл бұрын
This was incredibly useful. I wish I saw this before my interview last week lol
@VijayKumar-pd7vv
@VijayKumar-pd7vv 2 жыл бұрын
Good to see you regularly striving to keep up the good content
@sumansah7278
@sumansah7278 2 жыл бұрын
I have been learning react for front end developer but I did not encounter this much informative and concepts Thank you bhaiya.
@pawekoaczynski4505
@pawekoaczynski4505 2 жыл бұрын
I watch a lot of your videos and rarely comment, so I just wanted to take a moment to thank you for the job you're doing c:
@krishnendudasgupta3397
@krishnendudasgupta3397 2 жыл бұрын
This was really a cool and informative video.. Had worked with React previously and also had faced same errors and even felt disgusted when React threw that error... But after watching this one today, I am now convinced that React needs it for our good only and that is Optimisation of Rendering Process. Thank You so much for this video.. Would love to see this type of Out-of-Box content yet very important ones in future as well.... 😊😊😊
@soni.himansh
@soni.himansh 2 жыл бұрын
Loved the Explanation.
@sampathsam1049
@sampathsam1049 Жыл бұрын
Your the person , this what I need if i doesn't add keys what will happen . Your explained so good . Thanks bro
@kk-kw7yx
@kk-kw7yx 2 жыл бұрын
This format is really good. Tq!
@alibarznji2000
@alibarznji2000 2 жыл бұрын
We love you man, You're a very good teacher
@BeastMaster65
@BeastMaster65 2 жыл бұрын
Damn this was really informative, absolutely love it. Can you make another video on how to use useMemo and other rarely used hooks for optimization
@arjobansingh1940
@arjobansingh1940 2 жыл бұрын
Basically useMemo, is used to preserve the result of some complex function. As in functional components, we have some complex function, and on every re-render it recomputes itself, based on complexity of that function, it could really slow the component. As on every re-render that function is getting invoked and recomputing. So to get rid of this, useMemo is used, which memories the return value of some function, if the values used inside that function are same. For eg, in most of complex functions, values don't change often, but as it is present inside some component which ofcourse re-renders on every state and prop change, the function is basically called again and again, although values used inside that function have not changed. So in that case, useMemo is used, so that every time the component re-renders, useMemo will check if values it's function is dependant upon is changed? If yes, than only recompute the function, otherwise return the previous return value. As it will be same because, values that function is dependent upon has not changed. So basically this is the idea of useMemo, to learn about it's exact api, you can refer it's docs and other materials.
@tiffanyhall6701
@tiffanyhall6701 2 жыл бұрын
I loved this format. I enjoyed seeing your thinking as you wrote it on the board. Then the addition of the visuals you had were great at providing clarity and really making a point about what is important. I think your explanations were great for a student like me to follow. The only critique is the lighting. The reflection in your glasses and on the board is distracting. New fan here for sure!
@mpire6712
@mpire6712 2 жыл бұрын
This was incredible 👌👍
@fahimsium1013
@fahimsium1013 2 жыл бұрын
great content,keep up bro.
@kikevanegazz325
@kikevanegazz325 Жыл бұрын
Excellent tutorial
@gulshanaggarwal4577
@gulshanaggarwal4577 2 жыл бұрын
Seeing 👀 first time on the whiteboard, excited 🤩🤩
@ivanjasenov1220
@ivanjasenov1220 2 жыл бұрын
Great explanation and right to the point! +1
@html8525
@html8525 2 жыл бұрын
Great explanation
@pranupranav5563
@pranupranav5563 Жыл бұрын
for using unique key we can use useId() hook like: let uniqueId=useId() and use the uiqueId in the place of key of that element.
@JehadiHellTransportAgency
@JehadiHellTransportAgency Жыл бұрын
thankyou sir the best video for keys
@abhishekvishwakarma9045
@abhishekvishwakarma9045 2 жыл бұрын
Great Combo -> Theory concepts (White Board) + code explanation (In vscode) 🔥
@aadarshaacharya267
@aadarshaacharya267 2 жыл бұрын
Awesome video Mehul. Keep making whiteboard videos more. I find it more understandable than showing graphics presentation slides.
@cedric5027
@cedric5027 Жыл бұрын
Nice explanation. However, if the list is in a useState on the parent and one item is being updated, won't the whole tree be rerendered as the parents state has changed?
@divyanshsinghbhati6026
@divyanshsinghbhati6026 Жыл бұрын
0:46 In React, the Virtual DOM (Virtual Document Object Model) is maintained by the React library itself
@mohammedashraf8351
@mohammedashraf8351 2 жыл бұрын
Hey mehul, a great explanation
@rafeeqshaik7355
@rafeeqshaik7355 2 жыл бұрын
In-depth video of useref please. SPAM that like button BOIS
@Sonu-tg6tg
@Sonu-tg6tg 2 жыл бұрын
Thanks for such awesome videos, always waiting for your new videos, will be glad to get t-shirt giveaway
@Avoid0
@Avoid0 2 жыл бұрын
Wow🔥
@ridl27
@ridl27 2 жыл бұрын
okay, this tutorial is acceptable xD
@alihazem1479
@alihazem1479 2 жыл бұрын
Could you please upload a video in this way explaining useref pls? Would really appreciate you doing one!
@panchcw
@panchcw 2 жыл бұрын
we need these oversimplified whiteboard videos. for beginners, this will be a solid basic foundation.
@amarg26
@amarg26 2 жыл бұрын
@codedamn When an individual can decide when to switch from react.js to next.js. I have surface-level knowledge of function, class, hooks components. Can I switch to next.js or do I need to be an expert in react.js then move to next.js?
@aissaouboukioud585
@aissaouboukioud585 2 жыл бұрын
You can move to nextjs because it is build on top of reactjs and you can use everything you learn with reactjs the same way in nextjs. Nextjs comes with very great features out of the box like file-based router, server side rendering, creating endpoint API, ... So with Nextjs you can do fullstuck development
@abhishekvishwakarma9045
@abhishekvishwakarma9045 2 жыл бұрын
Yes, this needs to be answered 👍
@mitejmadan8672
@mitejmadan8672 2 жыл бұрын
I have created a complex project using nextjs. It is actually quite easy to learn next js if know react. Things which are different in next.js 1.) File architecture 2.) How css is applied. 3.) APIs 4.) Server side rendering. (If you want in your app). You can do the next js course from codedamn.. Some ytbers who you can follow 1.) Leigh Halliday 2.) Colby fayock 3.) Lee Robinson If you get stuck with an error check stackoverflow or the issue section from github nextjs.
@amarg26
@amarg26 2 жыл бұрын
Now it's @codedamn turn to answer 🤔
@arogueotaku
@arogueotaku 2 жыл бұрын
So based on the key, react will either update or not update the actual DOM. What if the data inside the array changes but the key does not change (if Using index as a key)? For example: [1,2,3] changes to [4,5,3] where the list is rendered using indexes as the keys. Will react not update the DOM in this case? Because keys are still the same (0,1,2)?
@codedamn
@codedamn 2 жыл бұрын
The example in video oversimplifies a few things. Think of key prop as linked to resetting the state of the component than a definitive flag for rendering. New rendering will always happen (until you memoize the component), however if your key remains same the internal component state will also remain same.
@siemen_subbaiah
@siemen_subbaiah 2 жыл бұрын
Next topic : Javascript hoisting or Javascript closures!
@KnownTechnical
@KnownTechnical 2 жыл бұрын
Nice
@thegreatprogrammer5937
@thegreatprogrammer5937 2 жыл бұрын
Next video plz: portal vs component in React
@hsngsoftware4380
@hsngsoftware4380 2 жыл бұрын
After learning from your course, I can start my own course, I AM GPT 10
@milon27
@milon27 2 жыл бұрын
how to work with jwt token in react + express api? how to store them so that we can prevent xss abd csrf attack.
@DEV_XO
@DEV_XO 2 жыл бұрын
nice
@milon27
@milon27 2 жыл бұрын
is it possible refresh jwt token in react? how to do that?
@neeleshpoojari5207
@neeleshpoojari5207 2 жыл бұрын
I HAVE ARRAY OF 3 ITEMS [1,2,3] AND I CHANGED IT TO [4,5,6] .. IN THIS SCENARIO INDEX IS GIVEN AS KEY.. HOW REACT WILL KNOW THAT VALUE HAS CHANGED IF COMPARISION IS BASED ON KEY?
@Sandeep-zd6dq
@Sandeep-zd6dq 2 жыл бұрын
I want codedamn t-shirt 🔥🔥 and want to jealous my friends in college 😂🥳🥳
@AbhishekKumar-dq5rj
@AbhishekKumar-dq5rj 2 жыл бұрын
But when using the index as key, if we delete an item from the start, then the key of trade of items still stays same, just the last key is deleted, so react should delete the last item, instead of re-rendering all the items from the beginning to the end
@btee23
@btee23 2 жыл бұрын
I used index as my key and had a nasty block scope bug. Lesson learned.
@m4saurabh
@m4saurabh 2 жыл бұрын
why react does not generate the keys by it self like svelte.
@lakshman587
@lakshman587 2 жыл бұрын
COMMENTED FOR T SHIRT GIVEAWAY!! :)
@cas818028
@cas818028 2 жыл бұрын
Yes stick with this format
@Fatima-ie5kj
@Fatima-ie5kj 2 жыл бұрын
Interviewers ask me this question n m stuck here i am ;(
@riteshpurwar4177
@riteshpurwar4177 2 жыл бұрын
Your writing on white board in same like me and I also thought people will not understand
@harshitgoyal19
@harshitgoyal19 2 жыл бұрын
To be honest, I am here for the GIVEAWAY 🤦‍♂️
@officialnizam
@officialnizam 2 жыл бұрын
random comment XD
@hemantharadhya8841
@hemantharadhya8841 2 жыл бұрын
hi, PLEASE DON'T add BACKGROUND MUSIC...it's a humble request.
@mysoregopinathshantanu8800
@mysoregopinathshantanu8800 2 жыл бұрын
May the like be on this comment.
@mohamedshuaau632
@mohamedshuaau632 2 жыл бұрын
Dom only cares about family
Why you should NEVER use index as key in React Lists
19:28
Code with Ahsan
Рет қаралды 1,3 М.
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 70 МЛН
Just try to use a cool gadget 😍
00:33
123 GO! SHORTS
Рет қаралды 85 МЛН
Must-have gadget for every toilet! 🤩 #gadget
00:27
GiGaZoom
Рет қаралды 11 МЛН
Understanding React's UI Rendering Process
29:07
CrossComm, Inc.
Рет қаралды 207 М.
React JS Lists and Keys | Learn ReactJS
32:54
Dave Gray
Рет қаралды 34 М.
React 18 Concurrent Rendering with createRoot - First look
9:12
Mehul - Codedamn
Рет қаралды 12 М.
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 60 М.
How React ACTUALLY works (DEEP DIVE 2023)
12:59
FrontStart
Рет қаралды 51 М.
Virtual DOM, Fibre and reconciliation
13:44
Hitesh Choudhary
Рет қаралды 20 М.
8 TypeScript Tips To Expand Your Mind (and improve your code)
10:54
Serverless to Homeless - Case study
10:28
Mehul - Codedamn
Рет қаралды 20 М.
10 React Antipatterns to Avoid - Code This, Not That!
8:55
Fireship
Рет қаралды 719 М.
git rebase - Why, When & How to fix conflicts
9:45
Philomatics
Рет қаралды 30 М.
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 70 МЛН