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

Preparing for Isomorphic JavaScript

Task 3: Tutorial for Video "Preparing for Isomorphic JavaScript"


Preparing for Isomorphic JavaScript

This tutorial introduces the concept of idiomatic JavaScript and prepares your application to align with best practices for isomorphic (or universal) JavaScript development. These practices make it easier to reuse code across different environments such as browsers, servers, or mobile applications.

Key Concepts Covered

  1. What is Idiomatic JavaScript?

    • Idiomatic JavaScript refers to writing code that is environment-agnostic.
    • Logic should not assume it is only running in the browser or interacting with the DOM.
    • Code should be modular, reusable, and environment-independent.
  2. Why Separate Logic from Views?

    • By moving logic out of view components, you improve reusability.
    • The same logic can then be executed on the server, in React Native, or any other platform.
  3. Preparing the Application

    • Move view-related logic into a central container, leaving the client-specific code focused on rendering.

Step-by-Step Guide

1. Refactor Your Application Structure

Create a dedicated App.js file to act as the central container for your application logic.

  1. Create App.js

    • Place it within the components directory: src/components/App.js.
    • Import required components such as Header, Portfolio, Footer, and others.
    import React, { Component } from "react";
    import Header from "./Header";
    import Portfolio from "./Portfolio";
    import Footer from "./Footer";
    
    class App extends Component {
        render() {
            const { data } = this.props;
            return (
                <>
                    <Header />
                    <Portfolio data={data.portfolio} />
                    <Footer data={data.footer} />
                </>
            );
        }
    }
    
    export default App;
    

2. Simplify the Client File

Update the client entry point to import and render the App component.

  1. Remove all logic and non-rendering code from client.js.

  2. Import the App component and pass data as props.

    import React from "react";
    import ReactDOM from "react-dom";
    import App from "./components/App";
    import portfolioModel from "./models/portfolioModel";
    import footerModel from "./models/footerModel";
    
    const data = {
        portfolio: portfolioModel,
        footer: footerModel,
    };
    
    ReactDOM.render(<App data={data} />, document.getElementById("root"));
    

3. Use Dynamic Models

Instead of hardcoding data, structure it into models. For example:

  1. Portfolio Model (portfolioModel.js):

    const portfolioModel = [
        { title: "Project 1", description: "Description of Project 1" },
        { title: "Project 2", description: "Description of Project 2" },
    ];
    
    export default portfolioModel;
    
  2. Footer Model (footerModel.js):

    const footerModel = [
        { link: "Home", url: "/" },
        { link: "About", url: "/about" },
    ];
    
    export default footerModel;
    

4. Pass Models Dynamically

When rendering components, pass the models dynamically as props. This allows easy swapping or updating of data without modifying component logic.

5. Debugging Errors

Ensure all imports and paths are correct. Avoid hardcoding paths such as ./components/App.js.


Key Takeaways

  • Idiomatic JavaScript ensures your application is scalable and flexible.
  • Separating logic and view layers reduces redundancy and enhances reusability.
  • Dynamic data models allow easier updates and facilitate isomorphic designs.

2024 Updates and Alternatives

  1. Use Server Components (React 18)
    React Server Components are a modern way to achieve idiomatic and reusable logic for SSR and SPA development. They further simplify the separation of logic from views.

  2. Explore Next.js
    If you're building isomorphic applications, consider using frameworks like Next.js, which offer built-in support for server-side rendering, API routes, and static generation.

  3. Leverage TypeScript
    For better scalability and type safety, use TypeScript to define interfaces for your models and props.

Example TypeScript Interface:

interface PortfolioItem {
    title: string;
    description: string;
}

By following these practices and utilizing updated tools, your applications will be prepared for future development challenges.

In the next tutorial, we’ll dive into building site navigation, integrating idiomatic practices into dynamic routing.

Video Transcript

00:00Welcome back to the final section of this title
00:04where we're gonna be building together a single
00:06page application.
00:08Through it, we're gonna be talking a lot about
00:10what Idiomatic JavaScript is.
00:13We're gonna talk about events
00:14and reacts, something we kind
00:15of avoided throughout the title so far.
00:18And last but not least, we're actually gonna build out a
00:21fully integrated single page application.
00:24Really cool stuff.
00:25So let's jump into the first lecture
00:27because in the first lecture we're gonna be preparing our
00:30environment to be idiomatic.
00:33Now what I'm saying idiomatic, what it actually means is
00:35that your JavaScript, especially now that we're working
00:38with React and we're not accessing directly the dumb,
00:43it doesn't need to know that it's the client application.
00:46Beyond that, we really wanna separate all the logic
00:49that could have been reused on a server, on a mobile device,
00:52or any other environment to stand on their own files.
00:56Although we're not gonna implement specific different types
00:59of JavaScripts if it's rendering on the server
01:01or rendering in different native environments,
01:04we're gonna show you how to separate your data logically
01:07so they'll be easy for you
01:08to then reuse the exact same JavaScript
01:10files whenever you need to.
01:13So let's see it in action.
01:15Okay, we're almost ready to move into the menu, but
01:18before we do that, let's, first of all,
01:20from our last section, let's just make sure
01:22that in our footer we undo that children
01:24so we don't get that air.
01:25And what I wanna do in this lecture is I want to go ahead
01:28and make our client more JavaScript idiomatic.
01:32Now what does that mean?
01:34Well, a lot of the logic that is inside
01:36of here could have run on the client
01:39and could run on the server.
01:40And in reality, what I would like to do is I would like
01:42to move all of the logic that relates to the view
01:46to its separate view container.
01:48So the only thing that really should be here is really
01:50the rendering section.
01:52So to do that, I'm gonna go into my components
01:54and let's go ahead and create a new file.
01:56And I'm just gonna call it that app js
01:58because it's gonna be our application now inside of our app,
02:02app app js.
02:04What I want to do is I want to basically create
02:07a components similar to all the components
02:09that we've created so far.
02:11So lemme go ahead and something similar to our header.
02:13I'm just gonna go ahead and duplicate our header here
02:17and let's just go into our app js.
02:21And I wanna render, of course, oh, I did render it a return
02:27and I wanna close that and I wanna close that.
02:30Beautiful. So the next step for me is to figure out
02:32how do I put this logic here.
02:34So to do that, I'm just gonna take a look at our client.
02:37And first of all, what I want to do is take anything
02:39that relates to a view that is not that line outside.
02:44So first of all, all of these are stuff that are happening.
02:47Let me just cut them.
02:48These are things that are related to all of our application.
02:51Again, going into our client.
02:53Let me go ahead and I'm just gonna delete here
02:54'cause you already have that in the sources there.
02:56I'm gonna delete out everything that's not re
02:58relevant for us right now.
02:59So I'm gonna go ahead and delete all that.
03:02This is very much relative to us.
03:04So let me just grab that and put that in our app here.
03:07And our app is literally gonna return that item,
03:10which in this case is basically a header, a portfolio,
03:13and about and a contact section.
03:16And we want to finish it all off with our footer.
03:19And by the way, did I have your
03:21any, let me just double check.
03:22Did I have your any model? Oh, I definitely did.
03:24So I don't wanna delete our model data.
03:26So let me just delete anything that is not our model data.
03:30Okay? So we don't need that,
03:32we don't need that, we don't need that.
03:34Beautiful. And we definitely want take our footer as well.
03:37So let me just take that footer
03:39and put it in here
03:40to our footer area so we're getting close.
03:43So first of all, we have everything here almost.
03:45We're just missing the data that needs
03:47to be sent through as well.
03:48But if we go back into our client here,
03:51what I'm gonna do now is instead
03:53of having this footer section, I'm just gonna go ahead
03:56and I'm gonna import, what am I gonna import?
03:58I'm gonna import the app itself. So we'll call it app.
04:01It's a default item that's being uh, loaded up.
04:03So let me just copy your one of those imports, call it app
04:08and call that app.
04:09And now I'm ready to go and load in our app.
04:14Now all that's left for me to do and I could delete all this
04:16'cause it's already inside of our app.
04:18All that's really left for me to do to get it
04:20to actually work is to send through this, these datas.
04:24So we have our portfolio model, we have our footer model.
04:27Let's go to notes, will make it all, you know,
04:29in the linguistics of the future,
04:31which let we use let instead of var.
04:34Now I'm gonna go ahead here and create another Let,
04:36and here what I wanna create is I'm just
04:39gonna call this our model.
04:40And in our model I want to basically have your an object
04:43that is just gonna list out different data sources.
04:46So first of all, our first data source will be portfolio
04:51and its value is gonna be our portfolio model.
04:54Beautiful. Our next item is our about.
04:59And obviously this is something that we would really want
05:01to do for uh, not about, we have your a footer model.
05:03So let's set that to be our footer.
05:06Okay, so we, so far this is all I've got,
05:08but really in reality I'd want to do it to all
05:10of our different sections.
05:12But let's go ahead and I'm gonna send in that data
05:15by creating a new property called data.
05:17And I'm just gonna send in that model making sure
05:19that I'm wrapping it with a curly bracket.
05:22Beautiful. Last
05:24but not least that's left for me to do inside
05:26of my app itself is just
05:28before I render things out
05:29to send in the correct information.
05:31So here would be this props, do data dot,
05:37uh, what was a portfolio.
05:40And the next one will be instead
05:42of footer model will be our footer.
05:45Now let's just see that everything still works
05:47and it should theoretically at least,
05:49but we have your little error.
05:50So let's see, what is our little, oh, big error.
05:53So if we go back here to, oh wow, there's a lot of errors.
05:56Okay, so here what can't resolve file directly of component.
06:01Oh, okay. Okay, fair enough. Why is that?
06:03That's because we are already in the component folder,
06:07so we don't need that components anymore.
06:09So we wanna strip that out, strip that out, strip that out
06:13and strip that out.
06:15Save. And let's give it another shot.
06:17And let's see, how many errors are we getting?
06:19And we have one error. Okay, one error is workable.
06:22Uh, we're having a problem with our data.
06:24So let's see our data por uh, yeah, port portfolio.
06:29So Port Folio and let's give it another shot.
06:34Beautiful. So we're getting everything.
06:36All that's really left for us is to create the menu,
06:39which we will do in the next lecture.
06:43In this lecture we really grasp grasped the idea
06:46of idiomatic JavaScript,
06:48although we're not going through the process of showing you
06:51how to implement in different environments,
06:53which will have other titles.
06:55And I'll have other titles covering exactly
06:57that about in general idiomatic.
06:59But it's something that you'll see throughout all
07:01of my React titles where we mention
07:03and we integrate idiomatic concepts.
07:06In the next lecture we're gonna build out our site
07:09navigation.

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!