Web Programming Anselm Spoerri PhD (MIT) Rutgers University

Slides:



Advertisements
Similar presentations
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Advertisements

Dawn Pedersen Art Institute. What is Spry? Spry is Dreamweaver’s version of JavaScript libraries. Spry effects alter the look of a page element—or of.
Lesson 12- Unit L Programming Web Pages with JavaScript.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
INTERNAL CSS Casey Ames Different types of CSS There are three different types of CSS: ◦ External CSS ◦ Internal CSS ◦ Inline CSS In this presentation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
DOM and JavaScript Aryo Pinandito.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
Lab 3: Language Structures User Interface Lab: GUI Lab Sep. 9 th, 2014.
JavaScript – The DOM JavaScript is object based The browser is object based – We can access the browser's objects in the same way we did JavaScript's Two.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
MTA EXAM HTML5 Application Development Fundamentals.
Introduction to Programming the WWW I CMSC Summer 2003 Lecture 9.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
XML DOM Week 11 Web site:
React Tutorial Giuseppe Attardi Università di Pisa.
Advanced Topics in Concurrency and Reactive Programming: HTML, CSS and DOM Trees Majeed Kassis.
Advanced Topics in Concurrency and Reactive Programming: React
6.1 Introduction 6.2 Element Positioning
Introduction to.
Web Programming Anselm Spoerri PhD (MIT) Rutgers University
HTML Elements with JavaScript and DOM Events
Week 3: Introduction to Javascript
>> JavaScript: Document Object Model
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Unit M Programming Web Pages with
MVC Architecture. Routing
Creating React Components with JSX and Babel
Week 4: Introduction to Javascript
Unit M Programming Web Pages with
Arrays and files BIS1523 – Lecture 15.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Not Sure how you Should React
DHTML Javascript Internet Technology.
Giuseppe Attardi Università di Pisa
Advanced Topics in Concurrency and Reactive Programming: React
Information Technologies Anselm Spoerri PhD (MIT)
>> Dynamic CSS Selectors
MIT GSL 2018 week 2 | Monday ReactJS II.
DHTML Javascript Internet Technology.
Introduction to Programming the WWW I
Working with Dynamic Content and Styles
© 2015, Mike Murach & Associates, Inc.
15 minute break.
JavaScript Programming Labs
Lecture Review What is a hybrid app? What does a UI framework do?
How to debug a website using IE F12 tools
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming Anselm Spoerri PhD (MIT) Rutgers University
Web Programming Anselm Spoerri PhD (MIT) Rutgers University
Web Programming and Design
Web Programming and Design
#04 Web Client (HTM5, React.js)
Web Programming and Design
Web Programming and Design
Information Technologies Anselm Spoerri PhD (MIT)
Information Technologies Anselm Spoerri PhD (MIT)
Build’an’Office add-in- using’modern JavaScript tools and techniques
SharePoint Saturday Kansas City October 19, 2019
Presentation transcript:

Web Programming Anselm Spoerri PhD (MIT) SC&I @ Rutgers University Info + Web Tech Course Web Programming Anselm Spoerri PhD (MIT) SC&I @ Rutgers University aspoerri@rutgers.edu

Quiz 4 – React.js Due Mar 31 (moved from Mar 17) Lecture 9 - Overview Recap – React.js Ex4 Lab Due Mar 31 Quiz 4 – React.js Due Mar 31 (moved from Mar 17) What to Do BEFORE Next Class Videos in Week 10 (in green)

Recap – React.js Library for Creating Interfaces Focus on the View Virtual DOM = JS Object: efficiently update & render components when data changes Components and subcomponents  nested structure (like HTML) One-way Data Flow States store what is happening in application and react to changes in state or data Props used to pass information between main component and subcomponents JSX = JavaScript as XML – write HTML tags inside JavaScript Combine the best parts of JavaScript with HTML tags JSX needs to be converted to regular JavaScript

Recap – React.js React = Components = UI element with changing data ES6 Class Component class MyComp extends React.Component { render () { HTML }} ReactDOM.render(<MyComp />, where in DOM)) Always start component names with capital letter render method for component Fairly simple nested JSX with {JS statement} Use external variable inline style = JS object CSS class  className background-color  backgroundColor ReactDOM – render elements in the actual DOM render(ReactElement, DOMElement) ReactDOM.render(<MyComponent />, document.getElementById("react-container"))

Recap – React.js – Properties and State Class component: this.props contains props defined by caller of this component {this.props.text} inside JSX <MyComp text="Hello World"/> {this.props.children} inside JSX <MyComp text="Hi">World</myComp>  Props used to pass information between main component and subcomponents Define state that can be queried and changed constructor(props) { super(props) //  access this.props in constructor this.state = { checked: false } // define state checked this.handleCheck = this.handleCheck.bind(this) // keep this in scope } handleCheck () { this.setState({ checked: !this.state.checked })} Note: if we want to use this.props inside of return () then need to work using vars States store what is happening in application and react to state / data changes constructor(props) { super(props) this.state = { x: y } this.funcZ = this.funcZ.bind(this)

Recap – React.js – Parent / Children Refs store & access actual value of user input component <textarea ref={input => this._newText = input}/> this._newText.value or <textarea ref="newText"></textarea> this.refs.newText.value Parent / Children Relationships – Create Subcomponents Create Main / Parent component: var Board = React Component this.state = { notes: array with content to add to subcomponents} render() { return (<div className='board'> { this.state.notes.map(this.eachNote) } </div>) } Nest inside of board div a series of Note components with property key

Recap – map / filter function and arrow (=>) function array.map() creates new array with results of calling function for every array element https://www.w3schools.com/jsref/jsref_map.asp array.filter() creates new array filled with array elements that pass test (provided as function) https://www.w3schools.com/jsref/jsref_filter.asp arrow function shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target.  https://www.lynda.com/JavaScript-tutorials/Arrow-functions/424003/459181-4.html var materials = [ 'Hydrogen', 'Helium', 'Lithium', 'Beryllium' ]; var materialsLength1 = materials.map(function(material) { return material.length; }); var materialsLength2 = materials.map((material) => { return material.length; }); var materialsLength3 = materials.map(material => material.length);

Recap – React.js – Update Children Update Children constructor(props) for Parent Component = array of objects {id: 0, note: 'Call Bob'} Create update method use ... as a "spread" operator to pass whole props object (all of its properties) https://reactjs.org/docs/jsx-in-depth.html update(newText, id) { this.setState(prevState => ({ notes: prevState.notes.map( note => (note.id !== i) ? note : {...note, note: newText} ) })) <Note key={i} index={i} onChange={this.update}> {note.note} </Note> save method for Note component needs to updated this.props.onChange(this._newText.value, this.props.index) // passed to update func

Recap – React.js – Remove Children Remove Children Create remove method: only keep notes don’t have id remove(id) { this.setState(prevState => ({ notes: prevState.notes.filter(note => note.id !== id) })) <Note key={ note.id} id={note.id} onChange={this.update} onRemove={this.remove}> {note.note} </Note> remove method for Note component needs to be updated this.props.onRemove(this.props.index)

Recap – React.js – Add Children Add Children this.state = { notes = [] } add(text){ this.setState(prevState => ({ notes: [ ...prevState.notes, { id: this.nextId(), note: text}] })) Need to add button to Board render() { return (<div className='board'> {this.state.notes.map(this.eachNote)} <button onClick={this.add.bind(null, "New Note")}>+</button> </div>) } <style> div.note {position: relative / absolute;} </style>

React.js – Component Lifecycle https://reactjs.org/docs/state-and-lifecycle.html Hooks for Creation, Lifetime and Teardown Mounting componentWillMount // load data or do things before DOM renders 1st time render componentDidMount Updating componentWillReceiveProps shouldComponentUpdate componentWillUpdate componentDidUpdate componentWillUnmount

Ex4 React.js – Create React App using Node.js Housekeeping https://www.lynda.com/ajax/course/645064/download/exercise/692567 In bulletin-board folder, select src folder Use: files from Exercise Files > Ch05 > 05_03> completed> bulletin-board > src Open Command Prompt: cd into the bulletin-board folder and npm start Fixed Bug in Week 8 Can’t remove all the notes (hint: what does index represent; role of id) Rename Functions CSS: div#react-container vs div#root Board: add  addNote | … | nextID  nextNoteID | … etc Note: edit  editNote | remove  removeNote | save  saveNote Board / Note: onRemove  onRemoveNote | onChange  onChangeNote Ex4 Overview Change note appearance when click on “Done” Add comments to a note + Remove comments from a note  Study code used to add and remove notes from board

Ex4 – React.js: Position + Button and Control Note Appearance Place + Button in top / right corner id="addNote" and position: fixed; top: 10px; right: 10px; Control Note Appearance via “done” checkbox Add checkbox in Note <input type="checkbox" onChange={this.handleCheckbox} defaultChecked={this.state.checked}/> handleCheckbox function calls setState to update checked state Want to change note appearance div className="note" style={this.style}> noteAppearance function: use noteBG to specify style obj to return renderDisplay: use … spread operator to create style obj var styleToUse = { ...this.noteAppearance()} <div className="note" style={styleToUse}>

Ex4 – React.js: Add Comment to Note Note component Add Comment placeholder below Note and style it Add “Add Comment” button in Note Specify onClick and addComment function onClick={this.addComment.bind(null, "Comment")} Add comments array as state of Note Create Comment component Create object with id and comment properties nextCommentId function Specify render function Add Comment component to Note eachComment function: create JSX for NoteComment {this.state.comments.map(this.eachComment)}

Ex4 – React.js: Remove Comment from to Note Add Delete button to Comment component <button onClick={this.removeComment}>X</button> Specify removeComment function: this.props.onRemoveComment (this.props.index) Specify onRemoveComment (index) this.state.comments.filter(comment => comment.id !== index) Need to update state of comments array Comment.remove calls Comment.props.onRemoveComment(index) which calls In Note component via <Comment onRemoveComment> removeComment function