Frontend Frameworks Part 2. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Similar documents
React. SWE 432, Fall Web Application Development

Lesson: Web Programming(6) Omid Jafarinezhad Sharif University of Technology

React + React Native. Based on material by Danilo Filgueira

React native. State An overview. See the tutorials at

React.js. a crash course. Jake Zimmerman January 29th, 2016

Xtern BOOTCAMP. Week 2 Day 1 May 22, 2017

Templates and Databinding. SWE 432, Fall 2017 Design and Implementation of Software for the Web

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. ReactJS

React Loadable: Code Splitting with Server Side Rendering

Gearing Up for Development CS130(0)

Down With JavaScript!

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

Table of Contents

React tips. while building facebook-scale application

React(.js) the Domino Way High-Performance Client for Domino. Knut Herrmann

Vue.js Framework. Internet Engineering. Spring Pooya Parsa Professor: Bahador Bakhshi CE & IT Department, Amirkabir University of Technology

Approaches to wrapping React. Thursday 3 September 15

React Not Just Hype!

Advanced React JS + Redux Development

1 CS480W Quiz 6 Solution

React & Redux in Hulu. (Morgan Cheng)

High Performance Single Page Application with Vue.js

Lecture 8. ReactJS 1 / 24

#06 RPC & REST CLIENT/SERVER COMPUTING AND WEB TECHNOLOGIES

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

State Management and Redux. Shan-Hung Wu & DataLab CS, NTHU

Building an OpenLayers 3 map viewer with React

widgetjs Documentation

Designing RIA Accessibility: A Yahoo UI (YUI) Menu Case Study

jquery Cookbook jquery Community Experts O'REILLY8 Tokyo Taipei Sebastopol Beijing Cambridge Farnham Koln

Interactor Tree. Edith Law & Mike Terry

Angular 2: What s new? Jonas Bandi, IvoryCode GmbH

Static Webpage Development

Front End Nanodegree Syllabus

Index. Boolean value, 282

Course Details. Skills Gained. Who Can Benefit. Prerequisites. View Online URL:

We will talk about Alt-Tab from the usability perspective. Think about: - Is it learnable? - Is it efficient? - What about errors and safety?

JQUERYUI - WIDGET FACTORY

AngularJS AN INTRODUCTION. Introduction to the AngularJS framework

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. 2.4 Event Dispatch 1

Introduction. Part I: jquery API 1. Chapter 1: Introduction to jquery 3

React Native. Animation

a Very Short Introduction to AngularJS

Multimedia im Netz Online Multimedia Winter semester 2015/16

Say No to Complexity!

WPF and MVVM Study Guides

Welcome to CS50 section! This is Week 10 :(

A4: HTML Validator/Basic DOM Operation

Testing & Performance. SWE 432, Fall 2016 Design and Implementation of Software for the Web

CITS3403 Agile Web Development Semester 1, 2018

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

Jquery Manually Set Checkbox Checked Or Not

Web Site Development with HTML/JavaScrip

A Brief Introduction to a web browser's HTML. and the Document Object Model (DOM) Lecture 28

Design patterns and Animation with jquery. with Paul Bakaus

JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

XML: Extensible Markup Language

AngularJS Fundamentals

REDUX: FP FOR THE WEB

AngularJS. Beginner's guide - part 1

jquery Tutorial for Beginners: Nothing But the Goods

JavaScript and Events

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. Event Architecture. A pipeline: Event Capture

Hacking with React. Get started with React, React Router, Jest, Webpack, ES6 and more with this hands-on guide. Paul Hudson.

An Brief Introduction to a web browser's Document Object Model (DOM) Lecture 20 COMP110 Spring 2018

Getting Started Guide

MARIA KERN NETZ98 GMBH MAGENTO 2 UI COMPONENTS THE JS PART

McLab tools on the web. Deepanjan Roy Supervisor: Prof. Laurie Hendren

Getting Started with ReactJS

DOM Primer Part 2. Contents

JQuery WHY DIDN T WE LEARN THIS EARLIER??!

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Frontend Frameworks. SWE 432, Fall 2016 Design and Implementation of Software for the Web

STARCOUNTER. Technical Overview

Ignite UI Release Notes

Event Dispatch. Dispatching events to windows and widgets.

Web Software Model CS 4640 Programming Languages for Web Applications

for Lukas Renggli ESUG 2009, Brest

International Research Journal of Engineering and Technology (IRJET) e-issn: Volume: 05 Issue: 06 June p-issn:

Index. Ray Nicholus 2016 R. Nicholus, Beyond jquery, DOI /

Model-view-controller View hierarchy Observer

IN4MATX 133: User Interface Software

Future Web App Technologies

TIME SCHEDULE MODULE TOPICS PERIODS. HTML Document Object Model (DOM) and javascript Object Notation (JSON)

6 th October 2018 Milan

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

Understanding this structure is pretty straightforward, but nonetheless crucial to working with HTML, CSS, and JavaScript.

JavaScript. jquery and other frameworks. Jacobo Aragunde Pérez. blogs.igalia.com/jaragunde

Theme System. Wisej Themes 1 OVERVIEW

1 Front-end Technologies - Historical Overview

! The final is at 10:30 am, Sat 6/4, in this room. ! Open book, open notes. ! No electronic devices. ! No food. ! Assignment 7 due 10pm tomorrow

Say No to Complexity!

Using the Actor Framework in LabVIEW

Review. Designing Interactive Systems II. Review. Base Window System. Apps UITK BWS GEL. 4-Layer Model Graphics and Event Library BWS GEL

JavaScript Specialist v2.0 Exam 1D0-735

University of Washington, CSE 154 Homework Assignment 8: Baby Names

Frontend UI Training. Whats App :

Stencil: The Time for Vanilla Web Components has Arrived

CSCI 201 Google Chrome DevTools

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

Transcription:

Frontend Frameworks Part 2 SWE 432, Fall 2017 Design and Implementation of Software for the Web

Today How to make React apps interactive Handling events, state, nesting components, reconciliation, controlled components In class activity 2

Reacting to change What happens when state of component changes? e.g., user adds a new item to list Idea 1. Your code updates this.state of component when event(s) occur (e.g., user enters data, get data from network) using this.setstate(newstate) 2. Calls to this.setstate automatically cause render to be invoked by framework 3. Reconciliation: Framework diffs output of render with previous call to render, updating only part of DOM that changed 3

What is state? All internal component data that, when changed, should trigger UI update Stored as single JSON object this.state What isn t state? Anything that could be computed from state (redundant) Other components - should build them in render Data duplicated from properties. 4

Properties vs. State Properties should be immutable. Created through attributes when component is instantiated. Should never update within component Parent may create a new instance of component with new properties class Welcome extends React.Component { render() { return <h1>hello, {this.props.name</h1>; State changes to reflect the current state of the component. Can (and should) change based on the current internal data of your component. 5

Working with state Constructor should initialize state of object constructor(props) { super(props this.state = {date: new Date( Use this.setstate to update state this.setstate({ date: new Date() Doing this will (asynchronously) eventually result in render being invoked Multiple state updates may be batched together and result in a single render call 6

Partial state updates State is an object, may contain whatever properties you add Can use setstate to update only parts of state you'd like to update fetchposts().then(response => { this.setstate({ posts: response.posts fetchcomments().then(response => { this.setstate({ comments: response.comments 7

Handling events class Toggle extends React.Component { constructor(props) { super(props this.state = {istoggleon: true; // This binding is necessary to make `this` work in the callback this.handleclick = this.handleclick.bind(this handleclick() { this.setstate(prevstate => ({ istoggleon:!prevstate.istoggleon ) render() { return ( <button onclick={this.handleclick> {this.state.istoggleon? 'ON' : 'OFF' </button> ReactDOM.render( <Toggle />, document.getelementbyid('root') https://reactjs.org/docs/handling-events.html 8

Functional components What happens if you have a simple component that only has properties and no state. Can use the function syntax to create a component. Component that is only a render method. function Square(props) { return ( <button classname="square" onclick={props.onclick> {props.value </button> 9

Nesting components UI is often composed of nested components Like containers in HTML, corresponds to hierarchy of HTML elements But now each element is a React component that is generated Parent owns instance of child Occurs whenever component instantiates other component in render function Parent configures child by passing in properties through attributes 10

Nesting components render() { return ( <div> <PagePic pagename={this.props.pagename /> <PageLink pagename={this.props.pagename /> </div> Establishes ownership by creating in render function. Sets pagename property of child to value of pagename property of parent 11

The data flows down State that is common to multiple components should be owned by a common ancestor State can be passed into descendants as properties When this state can manipulated by descendants (e.g., a control), change events should invoke a handler on common ancestor Handler function should be passed to descendents https://reactjs.org/docs/state-and-lifecycle.html#the-data-flows-down 12

The data flows down class Calculator extends React.Component { constructor(props) { super(props this.handlecelsiuschange = this.handlecelsiuschange.bind(this this.state = {temperature: '', scale: 'c'; handlecelsiuschange(temperature) { this.setstate({scale: 'c', temperature render() { const scale = this.state.scale; const temperature = this.state.temperature; const celsius = scale === 'f'? tryconvert(temperature, tocelsius) : temperature; return ( <div> <TemperatureInput scale="c" temperature={celsius ontemperaturechange={this.handlecelsiuschange /> </div> 13

Single page app In a single page app, there is only one single HTML page loaded by browser. When new views are opened by user or data arrives from server, client side JavaScript code generates new views. How could you build a single page app using React? 14

Activity: Tab widget In groups of 2 Build a tab widget in React Should have buttons or links to change the tab Should show and hide content based on which tab is active 15

What's wrong with this code? class Timer extends React.Component { constructor(props) { super(props this.state = { seconds: 0 ; this.interval = setinterval(() => this.tick(), 1000 tick() { this.setstate(prevstate => ({ seconds: prevstate.seconds + 1 ) render() { return ( <div> Seconds: {this.state.seconds </div> ReactDOM.render(<Timer />, mountnode 16

Component lifecycle class Timer extends React.Component { constructor(props) { super(props this.state = { seconds: 0 ; tick() { this.setstate(prevstate => ({ seconds: prevstate.seconds + 1 ) componentdidmount() { this.interval = setinterval(() => this.tick(), 1000 componentwillunmount() { clearinterval(this.interval render() { return ( <div> Seconds: {this.state.seconds </div> ReactDOM.render(<Timer />, mountnode 17

Component lifecycle class Timer extends React.Component { constructor(props) { super(props this.state = { seconds: 0 ; tick() { this.setstate(prevstate => ({ seconds: prevstate.seconds + 1 ) componentdidmount() { this.interval = setinterval(() => this.tick(), 1000 componentwillunmount() { clearinterval(this.interval render() { return ( <div> Seconds: {this.state.seconds </div> ReactDOM.render(<Timer />, mountnode ReactDOM.render(...) [component created] constructor(...) render() componentdidmount() tick() render()... [component rendered again by parent] componentwillunmount() [component created]... 18

Controlled Components class EssayForm extends React.Component { constructor(props) { super(props this.state = { value: 'Please write an essay about your favorite DOM element.' ; this.handlechange = this.handlechange.bind(this this.handlesubmit = this.handlesubmit.bind(this handlechange(event) { this.setstate({value: event.target.value handlesubmit(event) { alert('an essay was submitted: ' + this.state.value event.preventdefault( render() { return ( <form onsubmit={this.handlesubmit> <label> Name: <textarea value={this.state.value onchange={this.handlechange /> </label> <input type="submit" value="submit" /> </form> https://reactjs.org/docs/forms.html 19

Controlled Components Single source of truth Whenever a control changes its value React is notified State is updated Whenever state is updated If necessary, render function executes and generates control with new value 20

Reconciliation <Card> <p>paragraph 1</p> <p>paragraph 2</p> </Card> <Card> <p>paragraph 2</p> </Card> Process by which React updates the DOM with each new render pass Occurs based on order of components Second child of Card is destroyed. First child of Card has text mutated. https://reactjs.org/docs/reconciliation.html 21

Reconciliation with Keys Problem: what if children are dynamically generated and have their own state that must be persisted across render passes? Don t want children to be randomly transformed into other child with different state Solution: give children identity using keys Children with keys will always keep identity, as updates will reorder them or destroy them if gone 22

Keys function NumberList(props) { const numbers = props.numbers; const listitems = numbers.map((number) => <li key={number.tostring()> {number </li> return ( <ul>{listitems</ul> const numbers = [1, 2, 3, 4, 5]; ReactDOM.render( <NumberList numbers={numbers />, document.getelementbyid('root') 23

Todo in React class TodoApp extends React.Component { constructor(props) { super(props this.state = { items: [], text: '' ; this.handlechange = this.handlechange.bind(this this.handlesubmit = this.handlesubmit.bind(this render() { return ( <div> <h3>todo</h3> <TodoList items={this.state.items /> <form onsubmit={this.handlesubmit> <input onchange={this.handlechange value={this.state.text /> <button> Add #{this.state.items.length + 1 </button> </form> </div> https://jsfiddle.net/69z2wepo/57794/ handlechange(e) { this.setstate({ text: e.target.value handlesubmit(e) { e.preventdefault( if (!this.state.text.length) { return; const newitem = { text: this.state.text, id: Date.now() ; this.setstate(prevstate => ({ items: prevstate.items.concat(newitem), text: '' ) class TodoList extends React.Component { render() { return ( <ul> {this.props.items.map(item => ( <li key={item.id>{item.text</li> )) </ul> ReactDOM.render(<TodoApp />, mountnode 24