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

Merging Our Model with Our View – React SPA Development

Task 3: Tutorial Based on Transcript


Merging Our Model with Our View – Building React Single Page Applications

In this tutorial, we’ll explore how to merge the model and view to create a dynamic and scalable Single Page Application (SPA) in React. This step introduces a pseudo-controller by leveraging the client-side JavaScript logic to bridge the gap between your data (model) and user interface (view).


Step 1: Understanding the Role of the Controller

In React, while there is no explicit "controller" as in traditional MVC architecture, you can use the App or Client file to manage the logic for combining the model and view. This file acts as the business logic layer, organizing data and feeding it into React components.


Step 2: Define Models for Dynamic Content

Ensure each section of your app, such as the header, portfolio, and contact sections, has its corresponding model. Here's an example of a model for the header:

let headerModel = {
  title: "React SPA Development",
  subtitle: "Building Scalable and Reusable Applications"
};

Define similar models for other components like portfolioModel, aboutModel, and contactModel.


Step 3: Combine Model and View

To simplify the integration, define a structure that pairs each model with its corresponding view:

let modelViewMap = {
  header: { view: Header, model: headerModel },
  portfolio: { view: Portfolio, model: portfolioModel },
  about: { view: About },
  contact: { view: Contact }
};

Each entry associates a React component (view) with the appropriate data (model).


Step 4: Pass Data Dynamically to Components

Loop through the modelViewMap to dynamically render components:

let children = [];
for (let key in modelViewMap) {
  let { view: Component, model } = modelViewMap[key];
  if (Component) {
    children.push(
      <Component key={key} data={model} />
    );
  }
}

This approach ensures that views dynamically receive the correct data, making your app extensible and easier to manage.


Step 5: Refactor Component Props

To handle the new dynamic structure, refactor components to accept the data prop:

function Header({ data }) {
  const { title, subtitle } = data;
  return (
    <header>
      <h1>{title}</h1>
      <p>{subtitle}</p>
    </header>
  );
}

Ensure all components can unpack the data object.


Step 6: Render Components in the App

Finally, render the dynamically constructed children array in the App component:

function App({ data }) {
  return (
    <div>
      {children}
    </div>
  );
}

This approach simplifies adding or modifying components without needing significant changes to the App logic.


Key Takeaways

  1. Model-View Pairing: Use a modelViewMap structure to link data models to React components dynamically.
  2. Centralized Logic: Keep the logic for combining models and views in a centralized place like the App or Client file.
  3. Dynamic Flexibility: Refactor components to accept data props, making them reusable across different contexts.
  4. Scalable Structure: This approach allows for easy expansion as new components and models are added.

Next Steps

In the next tutorial, we will explore the ES6 Maps API to make the structure even more dynamic and introduce advanced ways of managing data in React applications.

For the full course and more tutorials, visit: React SPA Development Course


This tutorial bridges the gap between theory and practical implementation, giving you the tools to structure your React applications dynamically. Let me know if you need edits!

Video Transcript

00:00In real world applications, your model
00:03and view needs a controller.
00:05And although we are not building out a real full fledged
00:08controller, we're gonna use our component
00:11or our client folder, our client JavaScript file
00:15as our controller.
00:16And there we're gonna merge between our model and view.
00:20It's gonna be act as our business logic area,
00:23the controller, and
00:25through it we're gonna feed into our component, the data
00:28and the views that it's going to be using.
00:32Alright, let's take it, take it a step even further.
00:35We really want everything to be dynamic
00:37and we take a deeper look into our client and our app.
00:41One way we could do that is to merge between some
00:44of our content, which is the actual content itself,
00:48not the navigation, but our,
00:49not the footer either the header, the content, the contact,
00:53the about portfolio.
00:54Really all of those children, all
00:57of these elements we would love to be able
00:59to impose from our client.
01:02Basically creating a another variation not only
01:06of our model but also of our view, making it easier for us
01:10to add and change and,
01:11and configure creating with the same app structure.
01:15Different types of putting in,
01:17putting out different content.
01:18And I wanna show you how we're gonna do this.
01:20It's really exciting and it's getting close to the end
01:22of our title, which really takes it to the next level.
01:26So I'm just gonna make sure
01:27that I am importing it from the right directories.
01:31And once I have it imported from the right directories,
01:33I'm gonna take it a step further.
01:34And first of all, we have already our nav model,
01:36which we're going to go towards removing
01:39and turning it into something dynamic.
01:41We have our portfolio, we have our footer.
01:43We don't have a model for our contact, we don't have a model
01:47for our about, but for our header,
01:49if we take a look at our app, we do have a model,
01:52but we kind of baked it inside, which was our title
01:55and our subtitle.
01:56So I'm gonna go and just grab those subtitle
02:00and subtitle, go back into my client
02:02and let's just create for that another let,
02:05and I'm gonna let and call it our
02:07header model.
02:11And our header model will literally just have that title
02:16and it's gonna have that subtitle
02:19and that's all of our, the header model will have beautiful.
02:22So now that we have all the things that we're planning
02:24to turn into their content to be dynamic, what I want
02:27to do next is instead of creating a model,
02:30what I'm gonna do, and I'll just leave it there
02:32so you guys could look at it.
02:33What I really want to create is a model view. Structure.
02:36And our MV is basically, which in many ways our
02:40react needs access to the model and it has access to a view.
02:44The react itself is the view.
02:46So in this point in time,
02:47what I'm doing is I'm gonna mix between the two.
02:50I'm not gonna put our nav,
02:51but instead of that, I'm gonna jump first to our portfolio.
02:54'cause that is gonna be, well actually let's start from the
02:56order that we visually want to see it.
02:58So we want to have a header.
03:00And for our header, we don't have a model,
03:02we only have a view.
03:04What is the view? The view is the header.
03:07Okay, so let me just send that class as our view.
03:10Oops, what am I doing as our view?
03:14I'm gonna send in our header, okay, we got ourself,
03:17our header, what is the next item in our,
03:19and by the way, we could do it as an array as well.
03:21Um, I'm fine with it the way it is now,
03:23but we could have set it up as an array as well.
03:25So we have our header, the next item will be our portfolio.
03:29In the case of our portfolio, not only do we have our model,
03:35we also have our view.
03:36So let me just link our portfolio to our view.
03:39Making it now very easy.
03:40If I wanted to have different variations,
03:42maybe have one view for client side re generated, one view
03:46for another generated, and so on and so forth.
03:48So in my client itself, I'm defining the view
03:51of the portfolio.
03:52That makes sense 'cause I wanna make different views.
03:54Supposedly after our portfolio, we have an about,
03:58our about doesn't have a model, it only has a view
04:00and the view is our about.
04:03Okay, so let's put that in there.
04:05And last but not least, from the items that are going
04:08to be dynamic, we also have our contact.
04:11And our contact is view is contact. Beautiful.
04:15So we have our mv
04:16and what I'm gonna do is I'm gonna send my model
04:18and view as our data instead of our only our model,
04:24now that I'm sending it in, let's go inside of our app
04:26and start figuring out the logic set that we would want
04:29to have inside of our render.
04:30Well, the first thing that we're really, are, are gunning
04:33for is to really make this content more dynamic.
04:37So to do that, I'm just gonna empty it out
04:38completely as we're working.
04:40And let me just save that commented out
04:43and let's start figuring out our logic.
04:46So we know that we have a model and a view.
04:49So what I want to do is I wanna loop through those items.
04:52So a four loop works perfectly
04:55and I want to loop through those keys.
04:58So I'm gonna use key in this dot props data
05:02because I need access to that key.
05:03Okay? Now what I want to do is, first of all,
05:06I want find out if there is a class here.
05:08So I, I'm gonna call this view
05:10because that's the view that we want.
05:12And I'm gonna go to the, this props data key
05:17and try to gain access to that view.
05:19Now I'm gonna ask if there is a view, okay,
05:21so I found a class reference here.
05:23What do we wanna do in this case?
05:25Now there might not be maybe the, they didn't give us a view
05:28or it wasn't valid.
05:30In our case, if there is a view, what do I wanna do?
05:32I wanna push, well, let's create really a, uh,
05:35let's create a variable here.
05:36Let children, which is gonna be an array.
05:40And what I want to do is I want to go into our children
05:42and I want push a new child.
05:44And that new child is gonna be a view.
05:47Now, according to our structures that we had previously,
05:50what I'm gonna do is I'm gonna send in our data
05:53and our data is gonna be that model.
05:55So let's go ahead and fetch it This thought props, do data
06:00key model.
06:02Okay? So we have your our model. What else do we need to do?
06:06So first of all, we need to set a key
06:07because it's a dynamically added child.
06:09Lucky for us, we have access to that key as
06:12that is we're looping through that key.
06:14So we're creating our children.
06:15Let's go ahead and test it out
06:16and see if we actually gain access if
06:19everything is here working.
06:20So first of all, let's see if
06:21our children are coming through.
06:23So if I go ahead and give you a, a, a whirl, um,
06:26one is I'm getting a small error property.
06:28Uh, this, that title is undefined, which is
06:32probably an indication of the fact that our, um,
06:35title here is not being sent
06:37through even though I thought it is.
06:40So let's double check.
06:42Um, model that makes sense in our client.
06:46We have here our header that has a view,
06:49but it doesn't have a model.
06:50So we needed to add that model, right,
06:52that header, where are you?
06:55So we need to add here oto, uh, model.
07:01Beautiful. So now that I'm sending in the model view, I need
07:05to also make sure that in that actual view of our header,
07:10we are actually gonna, and let me, do I need these stuff?
07:13No, I don't need that.
07:14But one thing that I do need to make sure is that, um,
07:16'cause now I'm sending a data object
07:18and not that title directly.
07:19So let's go ahead and create here a new let
07:21and extract out the title
07:24and the sub title, um, out of what, out
07:28of this stop props data.
07:30And now that I've subtracted those out, I could go ahead
07:33and just approach them directly here
07:36and directly here so I don't have to,
07:38now they should be coming through instead of that data.
07:40Or I would type in also that da data. Okay?
07:43So once I've done that, let's go ahead
07:44and just test to see if it actually is rendering out.
07:46And if it does, we should see in our browser
07:49that it is rendering out.
07:50Beautiful. Okay, so we're getting really close now.
07:53The next step, and really the big, big last step that's left
07:56for us to do is to connect our menu and footer.
08:01In this lecture we, we separated
08:03or really reintroduce the model with the view inside
08:06of our controller, inside of our client folder
08:10or our app folder really depends on the situation
08:13where your application is.
08:15In our case, that's what we did In the next lecture,
08:17what we're gonna do is we're gonna make
08:19things even more dynamic.
08:20And as we introduce the ES six maps, API.

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!