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

Extracting the Model from the View

Tutorial: Extracting the Model from the View

In this tutorial, we focus on enhancing reusability by decoupling model data from React components. This principle aligns with the Model-View-Controller (MVC) pattern, where the model represents the data, the view is the user interface, and the controller handles the logic. By separating the data (model) from the view (React components), we can create more dynamic, maintainable, and reusable components.


Why Separate the Model from the View?

  1. Reusability: Components that don't have hardcoded data can be used in multiple contexts.
  2. Dynamic Applications: With external data, components can respond dynamically to changes.
  3. Simplified Maintenance: Decoupling data from components reduces dependencies, making the application easier to update and debug.

Steps to Extract the Model

  1. Create a Model:
    Define your model as a JavaScript object or array outside the component.

    const footerModel = [
        { title: "Location", content: "123 Main Street, Anytown, USA" },
        { title: "About Us", content: "We are committed to excellence." },
        { title: "Contact", content: "info@example.com" }
    ];
    
  2. Pass the Model as Props:
    Pass the model data into the component using props.

    <Footer data={footerModel} />
    
  3. Process the Data in the Component:
    Use the props to iterate over the data and render dynamic elements.

    function Footer({ data }) {
        return (
            <div>
                {data.map((item, index) => (
                    <div key={index}>
                        <h3>{item.title}</h3>
                        <p>{item.content}</p>
                    </div>
                ))}
            </div>
        );
    }
    

Building Reusable Components

To improve structure and reusability:

  • Create Sub-Components:
    Separate repeated elements into their own components, e.g., FooterItem.

    function FooterItem({ title, content }) {
        return (
            <div>
                <h3>{title}</h3>
                <p>{content}</p>
            </div>
        );
    }
    
  • Integrate Sub-Components:
    Replace the inline rendering with the sub-component.

    function Footer({ data }) {
        return (
            <div>
                {data.map((item, index) => (
                    <FooterItem key={index} title={item.title} content={item.content} />
                ))}
            </div>
        );
    }
    

Benefits of the Approach

  1. Ease of Debugging: Errors in data are isolated from component logic.
  2. Consistency: Ensures a unified structure for similar data-driven components.
  3. Extensibility: Adding new features or data types becomes straightforward.

Conclusion

By extracting the model data and processing it dynamically within components, you can build a more flexible React application. This approach is especially powerful in scalable applications where reusability and maintainability are key goals.

In the next lesson, we’ll explore JSX and ES6 spread features to further enhance the dynamic nature of our components.

Video Transcript

00:01This section is all about reusability.
00:03And one of those factors of reusability is making sure
00:06that your content within your component is
00:09not baked into it.
00:11And that's exactly what this lecture is gonna be about.
00:14And really, in many ways, our focus here is, you know,
00:17we're not gonna go into all of the structure of MVC,
00:20which is a very, very popular design pattern
00:23and react in many ways, acts as is an MVC
00:27of the visual layer.
00:29But we really don't want our actual model,
00:32our actual content, our actual data.
00:34If it's database data, if it's strings,
00:37whatever content it is, we don't want them baked into our
00:40components because we want our components to be reusable.
00:43So to do that, we want to extract our data
00:45and really make our application as dynamic as we can.
00:48That way in this lecture,
00:50we're gonna start extracting our model data outside
00:53of our menu.
00:56Alright, so let's give a name to this lead, obviously,
00:58let's call it a footer model
01:01because we're gonna take this model
01:03and we're gonna move it into our client.
01:05So that way we'll be able to recognize what it actually is.
01:09So we have your, our portfolio model,
01:11and let me just go ahead here and set up our footer one.
01:15And I'm gonna take our footer model
01:17and let's start by just sending it into our footer.
01:19So I'm gonna call this data
01:23and I'm gonna send in our footer mo model.
01:28Okay? Now that we have that, let's go into our footer
01:30and start figuring out how do we deal with this data.
01:33So as we said, we have here two types of data.
01:36One type of data that we'll have in the content, a string,
01:40and another type of data really will have in the content,
01:43in the, um, underlying child with it,
01:46with it will have an array, right?
01:47So we have an array or we have content.
01:49Now in our case, it's really important for us to secure
01:52that the content is valid,
01:54but we're gonna take a look at how do you secure validity
01:57of content in for, in this application after we set it up
02:01and, and configure it and create it.
02:04So let's go ahead into our footer.
02:05And in our footer, I'm gonna go into my render,
02:07but this time around, instead of rendering out this full
02:11instance, I want to kind of like isolate
02:13where are our components.
02:14So we have your one component
02:16and we have another component down below,
02:19which is in this very similar structure.
02:21Now we wanna actually turn this them into components.
02:24So I'm gonna go ahead and create another class.
02:26I'm not gonna even export it. I'm gonna call this class.
02:29How about, we'll call this, um, text footer
02:33or text component.
02:34It doesn't have to have the word footer in it
02:36because it's gonna be internal here.
02:38So let's call it, uh, um, text
02:41or simple, uh, text footer.
02:44And let's just call it text footer. Text footer.
02:46Again, it's gonna extend our react component.
02:50Not only is it gonna extend the react component,
02:52it's also gonna do the exact same thing by rendering.
02:56But what we're gonna do is we're gonna capture just that
03:00structure that we need right here.
03:01So I'm gonna go ahead and just copy it for a minute.
03:03And let's see what is the dynamic EL
03:06elements inside of here?
03:07So first of all, whenever we're gonna actually get to
03:10that footer content, we're gonna send it in.
03:12If we take a look at our client, what we're gonna do is
03:14we're gonna send in only the chunk that's related,
03:17which basically is our title and our content.
03:20So what I'm gonna do is I'm gonna go into my footer
03:23and I'm gonna change here to just replace that
03:26to be this props title.
03:29And I'm gonna go inside of here
03:31and I'm basically gonna put here are this dot props,
03:35do content.
03:37Now I'm assuming that the content is valid
03:39and it's already a string
03:41and this is the content that we wanna place
03:42inside the logic flow of checking.
03:44If it's the right one to call, it will happen inside
03:47of our footer structure.
03:48So now that we got, we set that up, we could go ahead
03:51and just delete that one from the row just so we're done.
03:54We, we know that we're done with it.
03:56Next item is let's go ahead
03:58and implement, um, let's go ahead
04:00and implement the structuring logic that would happen
04:03for those UI elements or the more complex data type.
04:07So I'm gonna go ahead and create here another class,
04:10and I'm gonna call it complex footer.
04:14And it's gonna extend our react.
04:19Let's just copy paste.
04:21So it's going to, it is gonna extend our react component.
04:24It's not gonna render this baby.
04:26Let's go ahead and just copy everything that is
04:28inside of that item.
04:29So if I go ahead here
04:30and I copy all this baby, chunk it right out
04:35and paste it right in here.
04:37Now, by the way, just when I was doing this one thing
04:39that I realized and noticed that there is kind of usability
04:42that I could recreate here, all of this is the same.
04:45The only difference is really what happens in this area
04:47where we have your paragraph
04:49or we have this more complex structure.
04:52So we could go and maybe create a facilitator class for this
04:55and use that facilitator class.
04:57That makes a lot of sense.
04:59So we could create here some sort of, uh, um, class,
05:02let's say, we'll call it footer element
05:07extends react component as well.
05:11But this time around we're gonna render,
05:14and by the way, did I close here?
05:15Everything that looks right. Did I close this well?
05:20Did I, oh no, it looks like I didn't look at that.
05:24You see, we, and then, and then we would have an error
05:26and that would be so embarrassing
05:28and, and it would be horrible.
05:29Okay, so let's continue, let's create a,
05:31let's create a footer element,
05:33or even let's just call it element,
05:34because we're assuming
05:35that this will only live within the bounds
05:38of this footer region.
05:40So no, we'll know what an element is
05:42because we're inside of this region.
05:44Okay? So what we wanna do is I'm gonna go ahead
05:46and copy this structure
05:48and let's see what is, what is the construct
05:50that we would probably have here?
05:51So first of all, title beautiful.
05:53And here we would probably put inside just children
06:00Props, do children.
06:02And now that we've set this basic construct
06:04that always repeats itself, I could literally, oh,
06:07and let's just make sure
06:08that we're doing it in the way that we're supposed to doing it.
06:11So we're, we're returning the item,
06:12we're closing out our class, just double checking,
06:15this class is closed and we have here an element.
06:18And now we could go ahead
06:20and just replace all of that with just saying element
06:23and just placing the content here to be basically, um
06:30hmm, basically just what's inside of you.
06:32Or we might need to also pass in
06:34that paragraph into that text element.
06:36So we'll, we'll, we might need to do that,
06:38we might need to revisit that.
06:40But the last thing also that we're gonna have
06:42to do is we want to pass through that title.
06:44So I'm gonna just go ahead here and set here.
06:46Uh, title equals
06:51This props title.
06:53So what I literally did here is I created a very basic
06:56element, which is gonna make it so much easier for me
06:59to pass through our content.
07:02So in this case, now all
07:03of this logic structure could change as well.
07:06And instead of this logic structure,
07:09we're gonna put our element
07:10and then we're gonna place this
07:12with the actual content itself.
07:14And in this case, our content itself is gonna be everything
07:18in this UL here.
07:19And I could take this ul, copy it out
07:23and place it right here.
07:25So o to the UL when it's a complex
07:28for footer element is gonna be rendering the exact same ti,
07:32same type of structuring.
07:35Now, there's a lot of ways for me to be able to do this
07:38for one is, right, we're using here our ul in our element.
07:42So we're sending in that ul to our element as well,
07:45which works for us.
07:46That works perfectly. This is an extended,
07:48basically everything is gonna go in here.
07:50And with that, with that it, so for our, um,
07:54for our footer text, all we really had to do,
07:56when we think about it, if we want to keep that paragraph,
07:58we could just set our paragraph and put that paragraph in.
08:02'cause really in this case, it's gonna take the child the
08:04content of whatever element
08:07and it's gonna pass, wrap it with a p
08:09and then send it to our element, which works brilliantly
08:12for our purpose.
08:14So now that we've set
08:15and created these structures, we could go,
08:18our complex footer still needs to be created.
08:20So, but for now, let's go ahead back into our footer element
08:24and figure out how to build this out.
08:26So I'm gonna just go into it
08:28and let's figure out, so first of all,
08:30we already have a module data that's coming through,
08:33but I really want to test things out.
08:35So let's start off by just putting one of the elements in.
08:38So let's start with, let's say we start with our text footer
08:41and with our text footer.
08:42I'm just gonna go ahead and set here a new text footer.
08:46And I'm gonna send in what it, what is it expecting?
08:49It's expecting a title. Oh, it's expecting a title.
08:54It's expecting content.
08:56And in our case, what we could do is we could take our
08:58properties out of the, out of the module.
09:01So basically in this case,
09:03I'm gonna go here into my text footer
09:05and I'll set your title.
09:07And let's say just for, for
09:08until we really test everything out,
09:09let's just type your ben for the title
09:12and for our content, I'm gonna put here it's me.
09:17Okay? And let's just, let's just see
09:19that we're not getting errors.
09:20That's a good place to start from.
09:21Let's just fix any errors if we have any errors going on.
09:24So first of all, am am I seeing any errors?
09:27No, but I'm also not seeing our footer elements,
09:29so let's see, why aren't we seeing it?
09:31We're seeing only the bottom part,
09:32but we're not seeing those elements inside of here
09:35that we really wanted to,
09:36or even though we're not getting an error.
09:37So that's already a good start.
09:39So let's take a, take a look at our text footer.
09:42And in our text footer, we're rendering the element.
09:45The element is literally this stuff.
09:48Now one thing that's is probably going on here
09:50that is not working,
09:51and if we just take a look at it,
09:53let's just take into inspect this area here.
09:55Really go into our footer.
09:57Let's see what's actually going on here.
09:58Above, uh, we have your container
10:01and the container has a row.
10:03Okay, so far so good,
10:05but the row is empty, so, okay, so we know, we know
10:07that the row is not getting rendered, so why not?
10:13Why not? Indeed. So let's think.
10:18Well, a good reason could be is
10:20because we never put into the footer.
10:22I thought we did. We must have put it somewhere else.
10:25Eh, there we go. We put it somewhere else
10:26that would explain it.
10:28So let me just undo whatever we did right
10:30here, we must have deleted.
10:32There's something that we didn't wanna delete.
10:36Uh, let's just test it out
10:40and see what, what, what damage did I do?
10:42So in my footer itself, I'm gonna go right in there
10:44and this time around I'm gonna send a footer
10:46and let's see what happens.
10:48There we go. Then it's me.
10:50And if I go ahead
10:51and I add another footer element, rip, rip,
10:55then we'll get three.
10:56It's mes on here. Yay.
10:58Alright, so in the next lecture, let, we'll continue down
11:00and actually connect all this data to be dynamic.
11:02But we're definitely getting close in this lecture.
11:06We continue down the path of making our each
11:09and every one of our components more reusable, more dynamic
11:12by extracting the model data out of our menu.
11:17In the next lecture, we're going to use the JSX
11:20and ES six spread features as we continue down this process
11:25of making everything dynamic.

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!