Lesson 4 - Modern JavaScript (JSX) Patterns

Modern JavaScript Programming with React

Libraries vs Frameworks

Libraries
Frameworks

Collection of reusable code

Predetermined architecture - you follow a specified pattern of development

Reduces need to write repetitive/complex things from scratch

You work within the boundaries set by the framework

You control how/when it's used. No/few boundaries.

"Right" and "wrong" ways to use it.

Similarities and Differences

Feature
Libraries
Frameworks

Control

You call the library's functions.

The framework calls your code.

Structure

Focused on specific tasks/tools.

Provides a full application structure.

Scope

Smaller, solves specific problems.

Larger, provides a complete solution.

Flexibility

Very flexible, use what you need.

Less flexible, must follow its rules.

Learning Curve

Generally easier to learn.

Steeper learning curve.

Web Dev Use

UI components (React, jQuery UI), utilities (Lodash), date/time (Moment.js).

Single-page apps (Angular, Vue, React with routing/state management), complex UIs.

Key takeaway:

  • Libraries: Tools you use within your code.

  • Frameworks: Structures you build your code within.


History

Part One: Functions in Modern JavaScript

Lesson Objectives:

  • Function expression and Function Hoisting

  • How Closure Works

  • Understand anonymous functions and their usage.

  • Explore commonly used array methods (map, filter, reduce, etc.).

  • Master arrow functions and their syntax.

  • Destructuring with JavaScript

  • Learn best practices for writing functions, especially in the React ecosystem.

Programming Activities for Part One

Preliminary Concepts

  1. Function Expression

  • A function expression in JavaScript is a way to define a function as part of an expression.

  1. Function Hoisting

  • Function hoisting is a JavaScript behavior where function declarations are conceptually moved to the top of their scope before the code is executed.

  1. Closure

  • A closure in JavaScript is a function that has access to variables from an enclosing (lexical) scope, even after the outer function has finished executing.

Advanced Closure Module Pattern with Data Privacy Encapsulation

The Advanced Closure Module Pattern enhances the classic module pattern by leveraging closures for robust data privacy and encapsulation.

Activity 1: Using Anonymous Functions

  1. What are Anonymous Functions?

    • Functions without a name.

    • Often used as callbacks in JavaScript.

  2. Hands-On Example:

    • Open your React project and modify a component as follows:

  3. Discuss:

    • Advantages: Concise and directly tied to their context.

    • Use cases: Callbacks for map, filter, and event handlers in React.


Activity 2: Commonly Used Array Methods

  1. Methods Covered:

    • map, filter, reduce, find, some, every, etc.

  2. Task:

    • Create a DataManipulation component:

  3. Explain:

    • How filter extracts specific items.

    • How map transforms an array.

    • How reduce combines elements

    • How find returns element


Activity 3: Arrow Functions

  1. Syntax:

  2. React Integration:

    • Update the ArrowExample from earlier to use arrow functions:

  3. Best Practices:

    • Use arrow functions for short functions and callbacks.

    • Avoid them in event handlers when this is required.


Part Two: Asynchronous Programming

Lesson Objectives:

  • Understand and use callbacks.

  • Use promises effectively.

  • Refactor code using async/await.

  • Learn React-specific async programming practices.

Programming Activities for Part Two

Activity 1: Using Callbacks

  1. Example:

  2. Discuss:

    • How callbacks work.

    • Issues: Callback hell.


Activity 2: Promises (better Solution to Callback hell)

  1. Task:

    • Refactor the callback code using promises:

  2. Explain:

    • How promises handle asynchronous tasks.

    • Benefits over callbacks.


Activity 3: Async/Await

  1. Refactor the Promises Example:

  2. Benefits of Async/Await:

    • Cleaner syntax.

    • Avoids promise chaining.


Activity 4: Destructuring Assignment

The destructuring assignment syntax is an expression that makes it possible to unpack values from arrays or properties from objects. It's a very common and useful feature in modern JavaScript.

Anatomy of main.jsx

  • open Chrome Developer Tools (by CTRL+SHIFT+I (or F12 key) and open Console to explore console log output.

  • Console › {type: 'h1', key: null, props: {children: 'Hello from createElement!'}, _owner: null, _store: {}}

Key Takeaways for Students

  • Modern JavaScript features, such as arrow functions, anonymous functions, and array methods, are essential for writing React components effectively.

  • Master asynchronous programming techniques, as React often involves handling async tasks (e.g., API calls).

  • Refactor code for clarity and maintainability, using async/await when possible.

Why React?

Lego Bricks Block Build

Vanilla JavaScrpt vs React

React's popularity stems from a combination of factors that make it a compelling choice for web development:

  • Component-Based Architecture: This promotes code reusability, organization, and maintainability, especially in large applications.

  • Virtual DOM: This enhances performance by minimizing direct manipulations to the actual DOM, leading to faster rendering and smoother user experiences.

  • Large and Active Community: This provides ample resources, libraries, and support for developers.

  • JSX: This syntax extension allows developers to write HTML-like code within JavaScript, making UI development more intuitive. It allows to create easily reusable and interchangeable "pieces of of the web" that can be combined in various ways to create complex systems

  • Strong Focus on UI: React excels at building interactive and dynamic user interfaces.

  • Declarative Programming : Focuses on what the program should do, and describes the desired outcome without specifying how to achieve it.

In short, React offers a powerful, efficient, and developer-friendly way to build modern web applications.

Last updated