React JS Lists and Keys | Learn ReactJS

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ค. 2024
  • Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
    In this tutorial, we will learn React JS Lists and Keys. You will discover every element in a list within ReactJS needs a key. You will also learn how to generate a dynamic list of elements within the JSX of React.
    🚩 Subscribe ➜ bit.ly/3nGHmNn
    🚀 This lesson is part of a Learn React tutorial series playlist:
    • React Tutorials
    🔗 Higher Order Functions tutorial: • Higher Order Functions...
    🔗 localStorage tutorial: • JavaScript LocalStorag...
    🔗 Source Code for this tutorial: github.com/gitdagray/react_li...
    React JS Lists and Keys | Learn ReactJS
    (00:00) Intro
    (00:05) Tutorial Setup
    (01:30) Changing the default state
    (03:02) View the new state in React Dev Tools
    (03:57) Displaying list items with map()
    (06:23) Lists of elements need keys
    (07:17) Previewing the React list
    (07:35) Adding the react-icons package
    (12:39) Applying CSS styles to the React list
    (15:20) Why we don't see a checkmark when we click
    (15:52) Adding an onChange listener
    (18:24) The handleCheck function
    (22:05) View the new component state in React Dev Tools
    (23:19) Saving state to localStorage
    (24:08) Adding an onDoubleClick listener
    (25:12) Adding conditional CSS styling
    (26:07) Adding an onClick listener
    (27:40) The handleDelete function
    (29:32) Adding an empty list message
    (32:07) When will we load state from localStorage?
    🚩 Quick note:
    1) In the tutorial, I save react-icons as a dev dependency like this:
    npm i react-icons -D
    ...but we're going to eventually need the icons in production, so you can save it like this instead:
    npm i react-icons -S
    ...if you saved it as a dev dependency, you can move it to production like this:
    npm i react-icons --save-prod
    🔗 ES7 React JS Snippets Extension for VS Code:
    marketplace.visualstudio.com/...
    🔗 React Dev Tools Extension for Chrome:
    chrome.google.com/webstore/de...
    🔗 Styled Components: styled-components.com/
    📚 References:
    ReactJS Official site: reactjs.org/
    React Wikipedia: en.wikipedia.org/wiki/React_(...)
    React Jobs: www.ziprecruiter.com/candidat...
    ✅ Follow Me:
    Twitter: / yesdavidgray
    LinkedIn: / davidagray
    Blog: yesdavidgray.com
    Reddit: / daveoneleven
    Was this tutorial about Lists and Keys in React JS helpful? If so, please share. Let me know your thoughts in the comments.
    #react #lists #keys
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  3 ปีที่แล้ว +12

    When you generate a dynamic list of elements in React's JSX, each element in the list needs a key attribute. This tutorial explores how to generate a list with the higher order function map() and apply the required keys to each element. In addition, this tutorial expands the project and builds out some additional functionality for the list items applying what was learned in the previous tutorials in the series. A link to the full Learn React playlist is above in the description. If you are new to Javascript, I recommend starting with my full 8 hour Javascript tutorial for beginners here: th-cam.com/video/EfAl9bwzVZk/w-d-xo.html

  • @psyferinc.3573
    @psyferinc.3573 ปีที่แล้ว +3

    im actually grateful. this time when i felt stuck i looked for your series . im excited to see react differently through your teachings.

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

    I've found your channel today and have already cleared all my doubts. Your examples are way easier to understand. Thanks a lot.

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

    You have a knack for breaking down the complex things into simple ones. Really loved these lectures. Thank you very much

  • @fazzyakamello
    @fazzyakamello 3 ปีที่แล้ว +2

    Dave i can tell you’re a natural teacher. The way you explain reason why you do certain things makes so much sense to me. Kodus

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

    your tutorial is top quality, extremely neat and to the point, helps a lot, great thx

  • @prasannapm3220
    @prasannapm3220 7 หลายเดือนก่อน

    Thank you Dave!

  • @caffeinated-pixels
    @caffeinated-pixels 3 ปีที่แล้ว

    I'm watching this series as a nice React refresher and to pick up some tips. Didn't know about React Icons, so thanks for that!

  • @jamshidtashkent1976
    @jamshidtashkent1976 ปีที่แล้ว

    I'm so happy to find your channel. Your videoes are very useful. Thank you so much.

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

    Every thing is explained with shortcut keys. I am feeling that in near future it turns me out as a cool developer. Bundle
    thanks, Dave Gray!🤩🤩

  • @khaledbenshible2657
    @khaledbenshible2657 ปีที่แล้ว

    it was rather complicate and not easy to learn but now im excited to see react differently through your teachings. It started to clear up little by little .great thx

  • @MohammadBarghamadii
    @MohammadBarghamadii ปีที่แล้ว

    Hi Dave, I don't know how to thank you. Thank you so much for your great tutorials.

  • @harag9
    @harag9 ปีที่แล้ว

    New to your channel, and just subbed. Great tutorial series, many thanks. I come from a old school html/JS/Jquery background - it really does look weird when I see "html like" (JSX) code within javascript. So far it seems more disorganised to me.

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

      I understand. I think you'll get used to it. Same happened to me!

  • @ioniangaming
    @ioniangaming 2 หลายเดือนก่อน

    Thanks Professor

  • @ahmad-murery
    @ahmad-murery 3 ปีที่แล้ว

    I read that keys are needed for react to detect the changes (add, update and delete) in a list but I know it worked for me without them and I was wrong,
    I just discovered that react will use list indexes for keys if not explicitly assigned which explain why id did work for me.
    With all my current workload, I feel like I still need to do my homework as a student,
    Thanks Dave,

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว +1

      Good note Ahmad. Yes, index should be available. Without explicit assignment of keys, I believe an error will show in the console while the JSX may still render. 🤔😃

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

    Hey Dave,
    I am following your tutorial and was confused with onDoubleClick event listener as it wasn't needed in my case. The only difference in my code is that I had to use htmlFor attribute for a label as otherwise I was getting an error.
    I installed eslint following some tutorial online and airbnb styleguide but that has many unexpected quirks, especially when following tutorials.
    Would be great to see a video about your working environment setup and yes I already watched your top 5 VS Code extensions for beginners.
    Great content and I love how you keep mentioning useful shortcuts when working with the code editor as it extremely helpful.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      Thanks for the notes! My working VS Code setup is pretty much what you see in the tutorials. I know some that add a lot to theirs, but I keep it very plain. The bracket pair colorizer 2 extension does help me and I usually keep prettier running, too.

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

      Thank you. I was wondering if you used any linting rules // style-guides but I guess is the best to keep things simple at the beginning as my example shows.
      Often you mention keyboard shortcuts but I might have missed how do you change multiple items at the same time Ctrl + D?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      @@notusedbrain you are right - Ctrl + D will select multiple instances and let you edit them all at once.

  • @oussamabouallati
    @oussamabouallati ปีที่แล้ว

    Thank you sir!

  • @oncoding4520
    @oncoding4520 ปีที่แล้ว

    Love your work

  • @user-ex1ks2xk5m
    @user-ex1ks2xk5m ปีที่แล้ว

    thanks Dave

  • @mohajeramir
    @mohajeramir ปีที่แล้ว

    I am rooting for you to reach the 100k milestone 💯

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

    Thank you for great tutorial.
    i change the status with a for loop.
    i want to know, is Maps performance is better then for loop? thank you.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      You're welcome! I prefer map. I haven't looked into the performance. I'm guessing if there is a difference, it would be very small.

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

    Thank you Sir!

  • @hanszoll4943
    @hanszoll4943 ปีที่แล้ว

    Thank you

  • @zakariachajia1199
    @zakariachajia1199 2 หลายเดือนก่อน

    I get more comfortable with react the more i advanced with this series

  • @ghostpieces2362
    @ghostpieces2362 2 ปีที่แล้ว +1

    Can you explain why we need to spread ...item in Line 24. I understand items.map loops through items and stores the object into item. We then check if item.id === id that was passed in. If True we need to spread item. This is where i'm confused. Couldn't we just say ? !item.check: item)'. Thanks

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

      Hi Steven, I'm going by memory as I have many videos, but you can provide a timestamp with your questions if you want to provide a specific reference. When you update the state of an object, you need to spread in all the properties. If you do not, you will erase them all. I go into detail with examples about this at the 10 minute mark of this video: th-cam.com/video/yvTGXH7uybA/w-d-xo.html

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

    11:40 - I've been developing HTML for a few years and it's only now I've come across tabIndex is. I'm assuming that keeping it at '0' means that the browser determines the tab order as opposed to the developer. Is that right?
    If you were going to set a tab index order, I suppose we could do so by using the curly braces and adding a reference to the index 'i' argument in the .map function. Is it a bad idea to set a tab order?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว +1

      Good questions! The assumption about how tabindex works is not quite accurate. A tabindex of 0 puts the element in the sequential tab order. More on tabindex here: developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex Thanks for the note! 💯

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

      @@DaveGrayTeachesCode Thanks. That makes sense.

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

      ​@@DaveGrayTeachesCode Is the reason you decided to add tabindex to the trashcans to improve accessibility for keyboard users? Thanks

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

    As a react beginner I can tell this a very important video

  • @GorritoCodes
    @GorritoCodes 4 หลายเดือนก่อน

    why yours .map works without a return statement, but mine doesn't?
    Thank you again!

  • @vigneshvicky113
    @vigneshvicky113 10 หลายเดือนก่อน

    Please explain handlecheck conditon

  • @vigneshvicky113
    @vigneshvicky113 10 หลายเดือนก่อน

    Please explain handlecheck condition

  • @Gintoki_Madao
    @Gintoki_Madao ปีที่แล้ว

    Commenting for the algorithm

  • @huyphamfc
    @huyphamfc ปีที่แล้ว

    What is your icon theme?

  • @rezamahmoudi4668
    @rezamahmoudi4668 7 หลายเดือนก่อน

    One request: can you repeat the development in the above part, with the useReducer?

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

    Excuse me, 20:23
    Q1:I can't understand what meaning is? { ...item, checked: !item.checked }
    I understand { ...item} or {checked: !item.checked }, but { ...item, checked: !item.checked }, I can't understand, can you explain it?
    Q2:Why use ...item? I know it is spread syntax.. but I can't understand why "...item" is work in the Ternary Operator?
    I've been thinking about it for a long time.
    (I have seen map(), Spread_syntax and Ternary Operator in MDN)

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +7

      If my memory is correct you are working with todo list items here. The object is one item. An item has multiple properties. When you see the spread operator here, we are spreading in the properties of the item to create a new object. The comma is added in order to add one more property to the object. If we add a property that already exists like the property checked, then the new property checked will overwrite the previous one. So in the example you are asking about, we spread in the properties including the existing checked property, but then we overwrite the checked property with the opposite value (true or false).

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

      @@DaveGrayTeachesCode thanks for the clear explanation! I had the question

  • @AbdulRazzaq-jy5qx
    @AbdulRazzaq-jy5qx ปีที่แล้ว

    Loved your teaching style. Just Amazing

  • @TravinskiyVladislav
    @TravinskiyVladislav ปีที่แล้ว

    Top

  • @1conscience0dimension
    @1conscience0dimension 10 หลายเดือนก่อน

    npm i -D is same as save dep not save

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

    const handleCheck = (id) => {
    const listItems = items.map((item) => item.id === id ? { ...item, checked: !item.checked } : item);
    setItems(listItems);
    above code is totally confusing for me why to ...item spread items could you explain in a bit laymen term????

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

      Hi Lavesh. When you first spread in item, it has all of the object properties. You follow it with a comma and then provide the checked property to overwrite the previous checked value. It is a true/false, yes/no type of value so when we say !item.checked, it sets it to the opposite of what it was before.

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

      @@DaveGrayTeachesCode thanks dave got it. :)

  • @dawid6026
    @dawid6026 ปีที่แล้ว

    7:17

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

    why is it parentheses() instead of curly bracket{} after arrow ?
    {items.map((item) => ( //why is it not curly bracket?


    {item.item}
    Delete

    ))}

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      That's a good question, and I realize it is different from what you typically see in Javascript functions. Parenthesis group multiple lines of code in a return. In your example, I used map() which is a higher order function. It returns values and the value being returned in this example is on several lines of code. I hope that helps!

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

      @@DaveGrayTeachesCode Thank you for your explanation!!

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

      @@aya2222 you're welcome! 🙏

  • @CodingBandit
    @CodingBandit ปีที่แล้ว

    welp now this is when it starts getting hard

  • @vigneshvicky113
    @vigneshvicky113 10 หลายเดือนก่อน

    I totally confused in this video