02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is

Making Things Dynamic with Maps

Final Tutorial: Making Things Dynamic with Maps in React

This marks the final step in our journey of building a reusable and dynamic Single Page Application (SPA) with React. In this tutorial, we harness the power of the ES6 Map API to finalize a dynamic structure for rendering components like navigation menus, footers, and other sections. This approach encapsulates everything we've learned in the course, culminating in a flexible and scalable SPA framework.


Step 1: Why Use Maps in React Applications?

The ES6 Map API offers a versatile way to manage key-value relationships, making it ideal for SPAs. It allows us to dynamically link identifiers (like footer or header) with their corresponding React components.

Advantages of Maps:

  • Keys can be objects, primitives, or even arrays.
  • Simplifies dynamic rendering logic.
  • Enables fallback to default components when custom ones are absent.

Example Usage:

const map = new Map();
map.set('footer', FooterComponent);
console.log(map.get('footer')); // Outputs FooterComponent

Step 2: Implementing Maps for Dynamic Rendering

We use the map to link identifiers to components and dynamically render them in our application.

  1. Define a Map in the Constructor:
    Create a map in your main App component to register default components like the footer.

    constructor(props) {
        super(props);
        this.map = new Map();
        this.map.set('footer', Footer);
    }
    
  2. Dynamic Fallback Logic:
    During rendering, check if a view exists for the section (like footer). If not, fetch the default view from the map.

    let view;
    if (!view && this.map.has('footer')) {
        view = this.map.get('footer');
    }
    
  3. Dynamic Footer Rendering:
    Use the resolved view to dynamically render the footer without hardcoding it into the application.


Step 3: Extending Dynamic Rendering

With maps, we can dynamically manage multiple sections, such as:

  • Navigation Menus: Assign views for different menu states.
  • Content Sections: Dynamically load and render views for About, Contact, or other pages.
  • Reusable Components: Swap components based on context, configuration, or user input.

Example: Default and Custom Views

if (!view && this.map.has(key)) {
    view = this.map.get(key); // Fallback to default view
}

Step 4: The Power of Reusability and Scalability

By using this approach, our SPA framework is now:

  • Dynamic: Components render automatically based on their configuration in the map.
  • Reusable: Default components ensure consistent behavior across the app.
  • Scalable: New sections or customizations can be added without altering the core logic.

Closing Remarks

Congratulations on completing this course! 🎉

We’ve journeyed through the principles of React and learned how to:

  • Build reusable components.
  • Implement idiomatic JavaScript practices.
  • Handle state and events dynamically.
  • Create a powerful, dynamic SPA framework using modern JavaScript tools like the ES6 Map API.

By applying these techniques, you’re now equipped to create scalable, maintainable, and reusable SPAs for real-world applications.


Next Steps:

Take what you've learned and start applying it to your projects. Experiment with different configurations, customizations, and explore how maps and other modern JavaScript features can streamline your workflow.

Thank you for joining this journey. We look forward to seeing what you'll build next!
For more advanced topics, visit the full course library.

Video Transcript

00:01To enable our map to be even more dynamic.
00:03We're going to use, not map our,
00:05our menu to be more dynamic.
00:06We're gonna use the map API. So that's what we're gonna do.
00:09We're gonna get introduce ourselves to ES six map.
00:12A-P-A-A-P-I.
00:14I really like the map, API, let's see it in action
00:17as we use it to data structure our SPA site.
00:23Okay, so let's continue and reconnect our footer as well.
00:26And if we remind ourselves inside of our client,
00:30we already created a model for our footer,
00:32but we haven't been using it in the last round.
00:34So let's go ahead and use it by adding the footer into
00:38our elements.
00:39And for this case, I'm gonna keep
00:41that structure model, footer model.
00:44Now on contrary to maybe like, um,
00:47and this is really down to like
00:49how you would wanna process things,
00:50but when we take a look at our children,
00:52really in our case we had here that footer, which I kind
00:55of want to just hard code
00:56and make sure that it's always there
00:58and fetch the data that is being sent from our app.
01:00If the user didn't send the data, then it'll get a error.
01:03So in this case, it'll be that model.
01:05But the problem with this structure would do it this way if
01:08we, if someone would try to gain
01:10and send in to our footer a view as well,
01:14maybe someone wants to change a view at some point
01:16for some certain situation, then
01:18because I'm hard coding it here,
01:20then our footer would not actually render correctly.
01:23It would render twice, which is probably not what we want.
01:26So in this case, what we want to do is we need
01:28to basically configure and think, so we have our view here
01:32and I could say I could do here,
01:34like if we don't have a view, um, if there is no view,
01:39then maybe we want to do something, try
01:41to set the view up again.
01:43And if it's still there's no view,
01:44well then I guess we won't render it.
01:46So how about we try going it down that path?
01:48So let me just go ahead here and just delete that.
01:50We're not gonna implement our footer, um,
01:52and um, statically, but we want it to be set dynamically.
01:57Now, to get that to work, what I'm gonna do is I'm gonna use
01:59something that is new to ES six, which is called maps.
02:04So while we're not gonna go into the intricates of
02:06how maps work in ES six, I'm gonna just create a new map
02:10inside of our constructor and I'm gonna call it new map.
02:13Now maps in generally speaking, are incredibly powerful
02:17because they enable us to connect different objects
02:21or strings or whatever.
02:22We really want two directional,
02:24one directional links ly a key and a value.
02:27But contrary to objects that had to have primitive values
02:30as their keys, maps could have complex values as their keys.
02:35So in this case, for example, we're gonna start off
02:38with something simple of map this map dot set.
02:41And I could then ha set there in that set of value.
02:44So for example, footer is the, the value that I'm looking
02:47for and waiting for.
02:48And really what I wanna do is I wanna link it
02:50with the footer class.
02:52Okay? So this will expose, so anytime someone uses footer,
02:55I'll have access to the footer class. Now
02:58That I'm gonna have access to this foot
03:00or class, I could go ahead back into our rendering if there
03:03is no view, and I could say if there is no view
03:05and n this dot map dot has,
03:10and then send the key that we want to send.
03:12So in this case has that key.
03:14If it has that key,
03:15then let's go ahead and we'll set an only.
03:17If we didn't have a view already, then I'm gonna go
03:19to this map, get based on that key,
03:23and it's gonna return me based on that key,
03:25it's gonna return me that class.
03:27So now I'm gonna have OSU a view set up.
03:29So now if I save this
03:30and just double check, we set our footer,
03:33we set inside of our app.
03:35If I go ahead in my application,
03:36we should now also get a footer all the way on the bottom.
03:40And we sure are, so literally what's actually going on here
03:43is it's first checking to see if we have a class.
03:46It didn't, and if it didn't have a class,
03:48but in our map we have a link to one of those classes.
03:51Then we're going to use that class by creating this logic,
03:55um, enabling to have default classes, similar
03:58to default properties
04:00that will be used if a class is not set.
04:02So for that point, if I wanted to, for example,
04:05I could go ahead and continue the setup
04:07and configuration, assuming that our application could have
04:12or should have other sections as well.
04:15But I'm not gonna do that 'cause I could set it for the,
04:16for the header, for the footer,
04:18for the contact for the about.
04:20But I'm gonna assume
04:21that whoever's gonna be loading this application is gonna
04:24send the content that they actually
04:25want in their application.
04:27Another assumption that I want to make is I want
04:30to make the assumption that the first item is gonna be
04:32that default homepage type of content,
04:35just like we saw in our menu and then the rest of the items.
04:38Okay? So in the next lecture we're gonna go ahead
04:41and integrate oto our menu.
04:43But this time we're gonna integrate the menu
04:45and turn that menu logic to be completely dynamic.
04:51In this lecture we used the map API
04:53and really started using it to build out that core logic set
04:57that will be used also for our single page application.
05:00In the next lecture, we're gonna finish up the job
05:03as we build out a complete SPA.

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!