Docsity

CSE 331 Software Design & Implementation, Lecture notes of Software Engineering

Partial preview of the text

Download CSE 331 Software Design & Implementation and more Lecture notes Software Engineering in PDF only on Docsity!

CSE 331

Software Design & Implementation

Andrew Gies

Autumn 2019

React Overview

HTML, Formally

  • HTML - Hypertext Markup Language
    • Not a full PL, describes document structure &

content

  • Consists mostly of tags and their contents
    • Each one has a beginning and end.
    • Can contain text (content) and other tags.
    • Each tag has a different meaning in the document.
    • Optional attributes (organized as key-value pairs)
      • Can think of them like “constructor

parameters”: pieces of data that contain extra

info about the tag.

  • Define document structure

Tags form a Tree

Some Text

Hello

div

p br div

p

This tree is often

called the "DOM" –

Document Object

Model

A Few Useful Tags

  • -

    - Paragraph tag, surrounds paragraph with whitespace/line breaks. -

    - “The curly braces of HTML” - used for grouping other tags. Surrounds its content with whitespace/line breaks. - - Like , but no whitespace/line breaks. - - Forces a new line (like “\n”). Has no content. - and and - Used to organize a basic HTML document. - - tag: creates a blank drawing surface that

you can “draw” on with JS

  • Create lines, shapes, draw images.
  • Has width and height attributes to determine

the size of the drawing surface.

  • We’re using in HW8 and HW9 to draw

lines/paths on top of images (like a map of campus!)

  • Javascript is going to need some kind of Canvas

object in order to call functions and draw pictures.

  • How do we get this object?

Example 2:

Drawing on a Canvas

Modifying HTML with JS

  • JS exists to allow webpages (meaning the HTML inside them) to change dynamically. So JS has to have a way to access/change the HTML tags.
  • Implementation: Every HTML element has an associated JS object that the browser maintains.
  • Can get a reference in JS by using the “id” attribute.
  • Every tag can have an ID - value is a string that uniquely identifies an element. HTML:

Hello, World!

JS: let parObj = document.getElementByID("thePar"); parObj.innerHTML = "Hello, 331!";

Making the Jump to React

  • Previously, we’ve been writing HTML, then using a small

amount of JS to interact with it.

  • In React: Write mostly JS, which is responsible for

dynamically generating the HTML on-the-fly.

  • Fundamentally different way of thinking about websites.
  • Allows code reuse (more or less impossible in HTML)
  • Improves modularity.
  • Designed to reduce coupling, increase cohesion. (Yay!)
  • Code looks different than what we’ve seen so far.

Starting React Theory

  • The webpage is made up of Components : these act like fancy tags: - Can contain other components - Have attribute-like things (slightly different, we’ll see later how they work). - Can also contain all kinds of JS code and application data (this is the powerful thing about components). - Decides what it “looks like” when actually placed on the webpage. - Expressed in terms of other components and regular HTML tags.
  • Create a component by creating a JS class that extends the Component class (provided by React)
  • A simple “Hello World” application in React
    • Demonstrates all the “starting” code required to get React up-and-running.
    • Uses React’s dialect of Javascript called JSX
  • React needs a “starting point” to work with when creating that application. We use index.html and index.js as that starting point. - “index” is traditionally used as the name of the starting point of any website. React looks for files with this name by default. (Like 'main' in Java)
  • The actual application traditionally starts in App.js

Example 3:

React Boilerplate

Let’s convert our previous canvas code from Example 2 to use React! Step by step from Example 3:

  1. Change the

    element to a

  2. Need to get a canvas object to draw like last time: different in React. a. It’s React’s job to manage the HTML for us, grabbing something with an ID defeats that purpose and can cause bugs. b. In React, we use “Ref” objects instead of ID strings, but they work similarly.
  3. Write an updateCanvasImage() method to draw a rectangle on the canvas like before.
  4. Use componentDidMount() to find out when React is ready for us to start drawing things, then call updateCanvasImage()

Example 4:

React Canvas

Going to use buttons (same buttons we’ve seen before) to dynamically change the state:
  1. Put the inside a so we can add things to our component.
  2. Add a few s to the component next to the canvas.
  3. Write a few functions to be the click functions of all the buttons. a. Note: In React, onclick was renamed to onClick and works slightly differently. Pass it a function object which is then called, instead of just JS code inside a string. b. For reasons we’ll discuss later, need to use the “arrow function” syntax instead of the regular function syntax.
  4. Call this.setState() inside the onclick functions to change our state.
  5. Use componentDidUpdate() to be notified of when the state has changed.

Example 6:

Changing State with Buttons!

React’s Rules about State

  • Do not modify state without setState please :)
    • setState does more than just update the variable, it also tells React what you’re changing so React can do its job
  • State updates are requests - the update is NOT guaranteed to have completed when setState returns. - This means if you setState and immediately try to use it, this.state probably doesn’t have the new value yet. - This is what componentDidUpdate is for - React will let you know when state has changed so you can redraw your component. - Rule of Thumb: treat state as if it’s write-only unless you know you’re currently inside render, componentDidUpdate, or something called by one of those two.
  • Inside the component: show up as properties of a props object that’s passed into the constructor.
  • Outside the component: passed to a component using a syntax similar to HTML attributes.
  • Read-only inside the component.
  • Changes (from the parent) trigger a component update just like state changes.
  • Main Idea:
    • State: the data is owned by the component itself.
    • Props: the data is owned by the component’s parent.

Props: The Other Kind of Data

1. Let’s create a new component: simply encompasses

a piece of text with the current color.

a. In a real application, probably isn’t something that makes sense to be its own component, but it’s a good example.

2. Need to somehow get the data from the App

component inside ColorTitle

a. Completely different classes/objects, so they

can’t just share variables.

3. Add a “color” attribute to our

declaration, which becomes a prop inside the

ColorTitle component.

4. When the “color” prop’s value is changed, React

automatically re-renders the component.

Example 7:

Introducing ColorTitle