General Assembly

React

Wilson Espina

Review

What did we do last lesson?

React

Learning Objective

Learning Objectives

At the end of this class, you will be able to:

  • Understand the roles of model, view, and controller.
  • Build a React component function.
  • Create a React component class.
  • Implement composition & reuse in a React app.
  • Install & use common React developer tools.

React

Agenda

  • Model View Controller (MVC).
  • React overview.
  • Creating React components.
  • Create React App.

React

Classroom Resources

React

Introduction to React

React

What is React?

React is JavaScript library from Facebook, that is designed to create interactive UIs.

React logo
  • Declarative: We describe the program/UI and React implements it (like ordering a pizza rather than making one).
  • Component-based: Build components, that manage their own state and structure them together into more complex UIs.
  • Virtual DOM: Maintains a representation of the rendered UI, that renders only the changed elements.

React

Virtual DOM

Virtual DOM
  • The Virtual DOM is a "virtual" representation of a UI in memory.
  • Any changes in state are detected in the Virtual DOM and a diffing algorithm finds the minimum number of operations required to update the real DOM.
https://auth0.com/blog/face-off-virtual-dom-vs-incremental-dom-vs-glimmer/

React

Model-View-Controller (MVC)

  • Model: data
  • View: user interface
  • Controller: coordinate between model and view
Shoe shop

React

Model-View-Controller (MVC)

MVC 1

React

Model-View-Controller (MVC)

MVC 2

React

Model-View-Controller (MVC)

MVC 3

React

Model-View-Controller (MVC)

MVC 4

React

Components

ITV site

React

Components

ITV site higlighted

React

React Developer Tools

React developer tools

Code along

Code Along
Code Along

React

Creating React Components

React

Functional Components

React

Functional Components


function Welcome(props) {
  return 

Hello, {props.name}

; }

React

Functional Components

Functional Component 1

React

Functional Components

Functional Component 2

React

Functional Components

Functional Component 3

React

Functional Components

Functional Component 4

React

Functional Components

Functional Component 5

Code along

Code Along

Open up: 00-functional-component-codealong

Code Along

React

JSX

JSX 2

React

Looping in React Components


const listItems = numbers.map((number) => {
  return 
  • {number}
  • }); // return statement in component return (
      {listItems}
    );

    React

    Looping in React Components

    
    [
      'cheese',
      'lettuce',
      'cucumber'
    ]
    								

    maps to

    
    [
      '<li>cheese</li>',
      '<li>lettuce</li>',
      '<li>cucumber</li>',
    ]
    								

    React

    c

    Looping in React Components

    
    [
      '<li>cheese</li>',
      '<li>lettuce</li>',
      '<li>cucumber</li>',
    ]
    								

    rendered as

    
    <li>cheese</li>
    <li>lettuce</li>
    <li>cucumber</li>
    								

    React

    Spread Operator

    Spread operator

    React

    Spread Operator

    
    props = {
      firstName: 'Mary',
      lastName: 'Richardson'
    }
    
    return <Greeting {...props} />;
    								

    is parsed as

    
    return <Greeting firstName="Mary" lastName="Richardson" />;
    									

    Code along

    Code Along

    Open up: 00-functional-component-codealong (PART 2)

    Code Along

    Exercise - Create Functional Components

    Exercise

    Key Objective

    • Build a React functional component

    Location

    • starter-code > 01-functional-component-exercise

    Timing

    10 mins

    1. Create variables storing references to the two new elements in the DOM.
    2. Create components to render the contents of the new state properties.
    3. Call the render method for each of your two new components.
    4. BONUS: Create and call a component function to render an image.

    React

    Class Components

    React

    Class Components

    
    class Welcome extends React.Component {
      render() {
        return (
          

    Hello, {this.props.name}

    ); } }

    React

    Class Components

    Class Component 1

    React

    Class Components

    Class Component 2

    React

    Class Components

    Class Component 3

    React

    Class Components

    Class Component 4

    React

    Class Components

    Class Component 5

    React

    Class Components

    Class Component 6

    Code along

    Code Along

    Open up: 02-class-component-codealong

    Code Along

    Exercise - Create Class Components

    Exercise

    Key Objective

    • Build a React class component

    Location

    • starter-code > 03-class-component-exercise

    Timing

    10 mins

    1. The start file contains the components we’ve already been working with, along with additional data in the state variable.
    2. Create variables storing references to the two new elements in the DOM.
    3. Create components to render the contents of the new data properties.
    4. Call the render method for each of your two new components.

    React

    Composition

    React

    Composition

    In programming, composition allows you to build more complex functionality by combining small and focused functions.

    • In the parent class, call each child with JSX using element syntax.
    • Pass necessary props as attributes, referencing this.props.
    • For child classes, move data manipulation outside of render() method, and reference the result instead.
    • Call ReactDOM.render() only on parent class.

    React

    Composition

    
    class Menu extends React.Component {
      render() {
        return (
          
        );
      }
    }
    							

    React

    Composition

    
    class Menu extends React.Component {
      constructor(props) {
        super(props);
        this.items = this.props.menu.map(function(item, index) {
          return <li key={index}>{item}</li>
        });
      }
      render() {
        return (
          
        );
      }
    }
    							

    OLD SYNTAX

    React

    Composition

    
    class Menu extends React.Component {
      items = this.props.menu.map(function(item, index) {
        return <li key={index}>{item}</li>
      });
      render() {
        return (
          
        );
      }
    }
    							

    UPDATED SYNTAX

    React

    Composition

    Composition 1

    React

    Composition

    
    class App extends React.Component {
      render() {
        return (
    							
          <div className="container">
            <Menu menu={this.props.menu} />
            <Heading title={this.props.title} />
            <Articles articles={this.props.articles} />
            <Footer footer={this.props.footer} />
          </div>
    
        );
      }
    }
    							

    Code along

    Code Along

    Open up: 04-composition-codealong

    Code Along

    Exercise - Reuse Components with Composition

    Exercise

    Key Objective

    • Implement composition in a React app

    Location

    • starter-code > 05-composition-exercise

    Timing

    20 mins

    1. Open CawCaw comp.png and examine the view you’ll be creating.
    2. Follow the instructions in script.js to build the User, Content, Date, and App components.

    React

    Create React App

    React

    Create React App

    • npm package
    • generates files & folder structure.
    
    npx create-react-app my-app-name
    								

    Code along

    Code Along
    Code Along

    React

    Learning Objective

    Learning Objectives

    • Understand the roles of model, view, and controller.
    • Recognise the primary uses of React.
    • Build a React component function.
    • Create a React component class.
    • Implement composition & reuse in a React app.
    • Install & use common React developer tools.

    React

    Look Ahead to Next Lesson

    • Lab Time for Final Project.

    React

    Q&A

    React

    Exit Ticket

    (Lesson #18)