Complete React Tutorial (& Redux) #14 - Outputting Lists

Net Ninja · Beginner ·🌐 Frontend Engineering ·7y ago

Key Takeaways

Introduces cycling through lists of data and outputting that data in React

Full Transcript

all right they're ninjas so so far when we're nesting this ninjas component down here for each one we're only outputting one individual ninja to the dot so we're passing the props of that ninja down to that component but instead it would be good if we could pass a list of ninjas down into this one component then inside that component we receive that list of ninjas cycle through them and output each ninja in this component so let's try doing that now since we're passing a list of ninjas down into one of these components we don't need to output them twice because they're all going to be going into the same single component and instead of defining that list as props on the component directly what I'll do is define that data up here in the States so we'll create a state's object and then we'll pass that property of the state down into this component so I'm going to create a property called ninjas and this is going to be an array now I'm just gonna paste this array of objects in right here so there's three objects each one has a name it has an age of belt color and an ID property and we'll come to that ID property and why I've had it later on but for now just worry about we have a name and age and a belt for each different ninja inside this array so each array item is an object okay so the idea is is that we're going to pass this array down into this component we're going to receive that array as a prop right here we're going to cycle through it and output some HTML for each individual ninja so then first of all let's pass this array through into this component as a prop so we'll call this prop ninjas like so and set that equal to this state dot ninjas so all we're doing is referencing this array right here and passing it down as a prop so now inside this component over here we will have access to that ninjas array so let's just grab that first of all I'm going to say cons ninjas is equal to this props and by the way this is just like saying Const ninjas is equal to this props dot names right it's the same thing it's just that we're using destructor in to do it so anyway we've grabbed that means property now off this props object so we have that array stored in here and now we want to output it to dot we want to cycle through those ninjas and output some HTML for each individual one which looks like this so how are we going to do that exactly well the way we're going to do it is by using the JavaScript method called map and what the map method does in JavaScript is taken array and it maps that array to a new array and that's what we want to do right we want to take this array and we want to map it to a new array which will be an array of HTML elements one for each ninja so we're going to return some kind of small template like this for each individual ninja in this array and we're going to store all of those snippets in a new array then we can output that array of code right here in the template so let's do this up here just below where we grab the ninjas will create a new constant and we'll call this ninja list so this ninja list right here this is ultimately what we want to store our new array full of HTML elements inside so we'll set that equal to ninjas which is the original array dots map and inside we take each individual ninja as we cycle through it and this is an arrow function right here and we perform a function on that individual ninja now we return a value inside this function right here and this return value is the element which is popped into the new array so right here so what we want to do is return some JSX code some HTML for each individual ninja and that is going to be the template from that individual ninja now that template is going to look very much like this thing down here so let's grab that and paste it up here all right so let's scoot that in so now then what we're doing is cycling through our ninjas array we're mapping it to a new array and for each ninja inside this array what we're doing is returning a bit of template for that ninja so now our new array is going to like an array of these things right here one set of JSX for each individual ninja alright so what we want to do is instead of referencing just name age and belt we need to reference ninja name because this is the item that we're cycling through each time around so we get that individual ninja each time so we can say Ninja Dame ninja age and ninja belt alright so that's pretty cool we're returning this JSX and we're storing that JSX for each ninja inside this array now so all we need to do is output that array down here so first of all let's create a div with a class of ninja - list like so and inside that div all I'm gonna do is output this array because that's going to be the list of elements so let's output that in our curly braces since this is dynamic content we're output it so ninja list which is just a variable name right here which is storing the array of this stuff for outputting that and react and JSX is going to know that we want to output this sequentially so for each ninja that's stored in this array now we want to output this template code and it's going to do that for us so let's save this and head over to the browser okay and now we seek for each ninja we get that template code we've mapped through those created the new array where each element inside that array is that snippet of JSX for each ninja and we've output each one when we output that array right here now this is almost on but if we go to the console we should get some kind of warning or error and you can see this error right here each child in an array or iterator should have a unique key prop so what it's saying is each one of these here this should have a unique key identify this surrounding element right here and that is so react can identify which ninja this is because if you think about it when we change this data over here react is going to update the template and recycle through the list so determine which ninja was removed and so therefore it can remove it or add it to the Dom so what it's saying is for each ninja when we have this template I want you to give the surrounding element right here a unique key that is specific to that ninja so if you delete that from the state at some point in the future then I know which one to delete from the list right here and it can do that efficiently then it can perform that manipulation of the Dom very efficiently if we give this a unique key so to do that that's simple all we do is say key is equal to something and this something has to be unique for that ninja so that's something is going to be this ID so you can see here each one has a different ID so all we need to do is pass that unique ID into this key field so ninja dot key like so and now the first one is going to have an ID of one the second one - the third one three etc so let's save that and view this in a browser and we still don't have a unique key so let's just go back and that's because stupidly I said ninja key and hot ninja ID which is actually the property named ninja ID so let's save that again and view it in a browser and now we don't get that warning over here so that's all fine err okay so that's how we can cycle through data we get the original array which is here ninjas we can map through that array we receive the individual ninja which represents each individual item in that array then we perform a function for each individual ninja inside that function we return a bit of JSX which we want to output for each individual ninja so it's cycles through those stores that JSX in a new array called ninja list over here then finally we output that ninja list down here and it outputs that whole list of JSX for us so that we got a pretty simple right that's how we loop through data and output it in react

Original Description

Hey gang, in this React tutorial I'll introduce you to the concept of cycling through lists of data and outputting that data in the component template. To do this, we'll use the map function. ---------------------------------------- 🐱‍💻 Course Links: + VS Code editor - https://code.visualstudio.com/ + GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist/tree/lesson-14 + React CDN - https://reactjs.org/docs/cdn-links.html ---------------------------------------- 🤑 Donate @ https://www.paypal.me/thenetninja 🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →