02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Dynamic Children from Model Data

Dynamic Children from Model Data: React and ES6 in Action

In this lecture, students learn the importance of separating concerns in application development by extracting model data from components, aligning with the MVC (Model-View-Controller) architecture. This step not only enhances code organization but also fosters scalability and reusability in applications.

The session introduces the concept of a "model" that stores dynamic data, such as portfolio items, separately from the React components responsible for rendering the view. By doing this, components focus solely on presenting the data, making them cleaner and easier to maintain.

A key highlight is the use of ES6's for...of loop to iterate over arrays, showcasing a modern and efficient way to process dynamic data. The lecture also demonstrates how to create child components dynamically by passing the extracted model data as props. Students see the application of keys to uniquely identify child components, addressing React's warning system when rendering lists.

By the end of this lecture, students will have a clear understanding of how to:

  • Separate model data from React components.
  • Use ES6 features like for...of to loop through arrays.
  • Dynamically generate child components from external data sources.
  • Leverage React’s keys for optimized rendering.

This approach simplifies component logic, enhances application scalability, and equips students with the tools to handle real-world dynamic content scenarios effectively. In the next lecture, students will explore ES6 variable definitions with var, let, and const to further modernize their JavaScript knowledge.

Video Transcript

00:01So far we were working in components, assuming
00:03that they could do really everything.
00:05They're the logic, their, the view there, the data itself.
00:10But react really d derives out of the concept
00:13of MVC model view control, where the model is,
00:17where your business data lives, the logic itself, the things
00:20that are coming from the database, the content that needs
00:23to be actually dynamic.
00:25The view is react, it's the visual element
00:27that goes on your screen and your controller.
00:31There's a lot of controllers.
00:33A component by nature is o to a type of controller,
00:36but it's a visual controller.
00:38So you would put put inside
00:39of a react component controller logic that is related
00:43to views only.
00:45While anything that's related to your model,
00:47you would put in some sort
00:48of a model database structure logic set,
00:51and then you would have your business logic that would sit
00:53outside of React.
00:55But in our case, let's take it one step backwards
00:58by taking our content itself
01:00and extracting it out of our component, which is
01:03what we're gonna do in this lecture.
01:05As we do that, we'll explore also the four of loop,
01:09which is new to ES six.
01:14The next step I want to do is
01:15extract a little bit of the data.
01:17And so far if we take a look at our portfolio,
01:20really we'll notice that I'm using that portfolio item.
01:23I've exposed it. Now the only place
01:25where it would make sense for me to expose
01:27that item is if we really had some sort of level
01:30of customization or maybe we would wanna let the user decide
01:34or the whoever's using this portfolio
01:37to put different type of things.
01:38And not only portfolio items,
01:40but in our case we really only want to have portfolio items
01:44inside of our portfolio.
01:45So one way we could do that is literally just going to
01:48that portfolio item and not making it exposed.
01:51And instead of that sending in the information as data,
01:54which is exactly what we'll do.
01:56So let's go ahead and create a new variable name
01:59Oopsie a new variable name.
02:01And I'm gonna call this our portfolio model.
02:05And that's because it's gonna be our model data,
02:07which is a term that comes from MVC,
02:09which we'll talk about briefly shortly.
02:11Really, uh, generally speaking, everything we are working
02:15with in our um, components
02:18of React are the view in a model view control.
02:21The idea is it's a design pattern that says let's separate
02:25between our model, our view, and our controllers,
02:29and have them completely separate
02:30so we could control the flow of information more better.
02:34Now, obviously in the state of a component, a component
02:38by nature is the place where all of the visual,
02:42all the view related things aggregate to.
02:45So then our model information gets there, our controller,
02:49processed controller information gets to our component.
02:52And our component in essence could be a controller as well.
02:55Although in the case of React,
02:56we really try to make our views,
02:59Not controllers, just something that passes
03:01through information
03:02where we have our controllers externally.
03:04But let's go ahead and create our, uh, view.
03:07And what I'm gonna do, uh, our module
03:09and what I wanna do is I'm gonna create here just like
03:12a reference to those images.
03:13And you know what I already pre-created.
03:15So let me just go ahead
03:16and I'm just gonna copy those variables.
03:18And what I did was I literally just copied
03:20from that information.
03:22I copied our image and I just added random links by the way,
03:25to my sites to pact
03:27and also to one of my oldest sites, which you're welcome
03:29to check out, which I've created maybe
03:3120 years ago, almost already.
03:32And I don't even know if I've updated,
03:33I've updated maybe 15 years ago last time.
03:35But it's kind of cool to have this archaic version
03:38of my site that I'm maintaining
03:40by not letting the server die out.
03:42And um, so if you go to the cabin link,
03:44then you'll see our cabin link there.
03:46So with that said, I've created our module
03:49and literally all it is, is an array with objects
03:51that contain our data.
03:52Now what I'm gonna do next is for my portfolio,
03:55I'm gonna send in a variable,
03:56but this time around I'm gonna literally send it in the
03:59whole object instead of sending in just a string
04:02or just some sort of a, um, a, a, a primitive value
04:05so I could send it also objects.
04:07We haven't done it so far, but I definitely could.
04:09Now, once I've changed that, let's go into our portfolio
04:13'cause we're gonna have to change things around
04:14inside of our portfolio.
04:15First of all, we're not gonna be
04:17rendering our children anymore.
04:18Instead of that, we're gonna have to process
04:20that information that is coming in inside of our, um,
04:24basically inside of our props.data.
04:26So to do that, I'm gonna create a variable,
04:28I'm gonna call it children and I'm
04:30gonna make it as an array.
04:32And I wanna show you a completely new thing to E es six
04:35where I could do a forward loop
04:37and I could literally go ahead
04:38and for this item, I want to go out of this props.
04:43Oh, what the, what that I do. Props, do data. Beautiful.
04:47Now what I've just done here in E es six is literally cycled
04:51through each of the data entries with the name of item.
04:55And now that I've done that, I could go into my children
04:58and push one child at a time and I could go ahead here
05:02and push here a portfolio item sending in the data.
05:05So let's just send in this item in and close that
05:09and that will be our children.
05:10Now before we even render this, I'm, instead
05:13of rendering children, I'm gonna go ahead
05:14and render the children variable instead
05:17of the local variable that I've created.
05:19But also, let's go ahead into our portfolio item
05:21and change the logic now
05:22that we have a data being sent instead
05:24of a image being sent.
05:26So if we go into our portfolio item, I'm gonna go
05:29and search for wherever I'm actually referring to it.
05:32So it's props, dot data image.
05:35And I could go ahead also in our E four X area in
05:38our jsx more accurately.
05:40Um, and I could go inside here
05:42and search 4, 4, 4, 4, 4 our link, which is right here.
05:46And I could just delete that string
05:48and replace it with our data link.
05:52And there we go. We connected all the dots.
05:54I believe all that's left for us to do is to test it out.
05:58And if we go back into our browser and, uh,
06:00and refresh, we'll see that first of all,
06:02we have your small error, but all
06:03of our content is being rendered out just
06:05as it was previously.
06:07Now, the one thing that I want to do to sort to sort it out,
06:09by the way, it's taking us to the external links now are,
06:12uh, fade outs won't work anymore.
06:14They're gonna, they'll take us into those sites.
06:16Um, so what I wanna do is I wanna fix this error of the key,
06:20which is basically it's a warning that's telling me
06:22because I'm adding dynamically elements, I need to put
06:26inside a key and give them a unique name within the
06:29flow of that item.
06:30So within our portfolio, it needs to have a unique key.
06:34So in our case, I could take our children's length really
06:37and I could use that as some sort of a unique key.
06:39So I'm just gonna go ahead here
06:41and ask for the children length.
06:42'cause each one will be different.
06:44And I know that that's the only stuff
06:45that are being sent in.
06:47So that should resolve the issue
06:48of our children having the same key.
06:51Uh, I'm using undefined, which is not good. Um, length.
06:56There we go. And that should sort it out.
06:59And now we shouldn't get that error anymore. And that's it.
07:03That's basically oto.
07:04We've just learned in E four X how
07:05to use these really cool new four loops.
07:07And we've also seen how
07:08to make our content even more dynamic
07:10by using our external model for our portfolio component.
07:17In this lecture, we took a,
07:18we took our first steps into extracting our model data out
07:22of our component.
07:24In the next lecture, we'll continue down the path
07:26as we explore the different types of
07:29variable dis definitions that exist in E ES six,
07:32which are var let and const.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!