React Tutorial Giuseppe Attardi Università di Pisa.

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

Validation in Angular 1.3 And other angular tidbits.
React. A library for creating user interfaces. React Renders your UI and responds to events.
AJAX & By – Anupama Sharma. Defining Ajax Ajax isn’t a technology. It’s really several technologies, each flourishing in its own right, coming together.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
Object-Oriented Analysis and Design
JavaServer Faces: The Fundamentals Compiled from Sun TechDays workshops (JSF Basics, Web-Tier Codecamp: JavaServer Faces, Java Studio Creator; IBM RAD)
NextGen Technology upgrade – Synerizip - Sandeep Kamble.
JSP Standard Tag Library
MVC pattern and implementation in java
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
Java Beans.
Robert Fourer, Jun Ma, Kipp Martin Optimization Services Instance Language (OSiL), Solvers, and Modeling Languages Kipp Martin University of Chicago
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
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.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
JavaScript, Fourth Edition
Getting started with ASP.NET MVC Dhananjay
ANDROID AND MODEL / VIEW / CONTROLLER. Slide 2 Design Patters Common solutions to programming problems are called design patterns Design patterns are.
Martin Kruliš by Martin Kruliš (v1.1)1.
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
Introduction to Programming the WWW I CMSC Summer 2003 Lecture 9.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JQUERY AND AJAX
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Flux & React Web Application Development Mark Repka, Rich McNeary, Steve Mueller.
Customizing Share Document Previews Will Abson Senior Integrations Engineer and Share Extras Project Lead
AngularJS. What is AngularJS  Javascript Framework  MVC  for Rich Web Application Development  by Google.
Advanced Topics in Concurrency and Reactive Programming: React
THE DOM.
Introduction to.
Unit 4 Representing Web Data: XML
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
MPEG-4 Binary Information for Scenes (BIFS)
Creating React Components with JSX and Babel
CMPE 280 Web UI Design and Development November 28 Class Meeting
XAML User Interface Creation in C#
Understand Windows Forms Applications and Console-based Applications
Not Sure how you Should React
Karina Martinez Jessica Palacios Bryan Bravo Miguel Cayetano
Chapter 7 Representing Web Data: XML
DHTML Javascript Internet Technology.
Giuseppe Attardi Università di Pisa
Advanced Topics in Concurrency and Reactive Programming: React
MIT GSL 2018 week 2 | Monday ReactJS II.
More Sample XML By Sadia Anjum.
DHTML Javascript Internet Technology.
Introduction to Programming the WWW I
15 minute break.
JavaServer Faces: The Fundamentals
Presented by: Jacky Ma Date: 11 Dec 2001
Javascript and JQuery SRM DSC.
Tutorial 10: Programming with javascript
A JavaScript framework
Web Programming Anselm Spoerri PhD (MIT) Rutgers University
Web Programming Anselm Spoerri PhD (MIT) Rutgers University
Web Programming and Design
Web Programming and Design
Build’an’Office add-in- using’modern JavaScript tools and techniques
SharePoint Saturday Kansas City October 19, 2019
Presentation transcript:

React Tutorial Giuseppe Attardi Università di Pisa

What is React React is a JavaScript library for creating user interfaces React is a JavaScript library for creating user interfaces Simple Simple –React automatically manages all UI updates when underlying data changes Components Components –React reusable components facilitate code reuse, testing, and separation of concerns

Design Features React is not a MVC framework React is not a MVC framework No templates No templates –UI is made of components –React uses a real, full featured programming language to render views Virtual DOM Virtual DOM –lightweight description of the DOM

No MVC, just V In traditional MVC different parts of the UI talk via events, with a controller receiving all the user inputs, manipulating the models if needed and then calling the views and telling them to re-render if necessary. In traditional MVC different parts of the UI talk via events, with a controller receiving all the user inputs, manipulating the models if needed and then calling the views and telling them to re-render if necessary. In React, when a user interaction occurs, everything is re-rendered In React, when a user interaction occurs, everything is re-rendered Separation between code and presentation vanishes (again …) Separation between code and presentation vanishes (again …) Components are single units, they can be instantiated and moved without having to reconnect them to handlers (e.g. in jQuery.readyFunction) Components are single units, they can be instantiated and moved without having to reconnect them to handlers (e.g. in jQuery.readyFunction)

Virtual DOM Pure JS lightweight description of the DOM Pure JS lightweight description of the DOM Components keep a state, avoiding storing the state in the DOM Components keep a state, avoiding storing the state in the DOM Incremental updates Incremental updates –The data returned from render is neither a string nor a DOM node -- it's a virtual DOM representing what the DOM should be –reconciliationreconciliation

Incremental update implementation When a component is first initialized, the render method is called, generating a lightweight representation of the view When a component is first initialized, the render method is called, generating a lightweight representation of the view From that representation, a string of markup is produced, and injected into the document From that representation, a string of markup is produced, and injected into the document When data changes, the render method is called again When data changes, the render method is called again In order to perform updates efficiently, the React engine diff the return value from the previous call to render with the new one, and generates a minimal set of changes to be applied to the DOM In order to perform updates efficiently, the React engine diff the return value from the previous call to render with the new one, and generates a minimal set of changes to be applied to the DOM

React React JSFiddle React JSFiddle React JSFiddle React JSFiddle React Tutorial React Tutorial React Tutorial React Tutorial

JSX Children Text ; Children Text ; React.createElement("div", { className: "red" }, "Children Text"); JSX is a JavaScript syntax extension that looks similar to XML ; ; React.createElement(MyCounter, { count: }); className not class, reserved word in JS

JSX var scores = { player1: 2, player2: 5 }; Scores Scores </Dashboard>; React.createElement(Dashboard, { "data-index": "2" }, { "data-index": "2" }, React.createElement("h1", null, "Scores"), React.createElement("h1", null, "Scores"), React.createElement(Scoreboard, { React.createElement(Scoreboard, { className: "results", scores: scores }) className: "results", scores: scores }));

Transform Test JSX transofrmation to JavaScript at Babel REPL Test JSX transofrmation to JavaScript at Babel REPLBabel REPLBabel REPL

Components var MessageComponent = React.createClass({ render: function() { render: function() { return {this.props.message} ; return {this.props.message} ; }});ReactDOM.render(,, document.body document.body); They must implement the function render

Props When a component is rendered, it can access its attributes using this.props. When a component is rendered, it can access its attributes using this.props. {this.props.message} ; {this.props.message} ;

Excercise var VacancySign = null; ReactDOM.render( Replace me, Replace me, document.getElementById('container') document.getElementById('container')); The component VacancySign should render a div with either the text "Vacancy" or "No Vacancy" depending on the prop hasVacancy. View Solution

Events var BannerAd = React.createClass({ onBannerClick: function(evt) { onBannerClick: function(evt) { // codez to make the moneys // codez to make the moneys }, }, render: function() { render: function() { // Render the div with an onClick prop (value is a function) // Render the div with an onClick prop (value is a function) return Click Me! ; return Click Me! ; }});

Exercise var ChildComponent = React.createClass({ render: function() { render: function() { return return Do Magic // invoke performMagic in parent Do Magic // invoke performMagic in parent ; } ; }}); var ParentComponent = React.createClass({ performMagic: function() { alert('TAADAH!'); }, performMagic: function() { alert('TAADAH!'); }, render: function() { return ; } render: function() { return ; }}); ReactDOM.render(, document.getElementById('container') document.getElementById('container')); View Solution

State A state is internal and controlled by the component itself while props are external and controlled by whatever renders the component. See example. example

Like Button var LikeButton = React.createClass({ getInitialState: function() { return { liked: false }; }, getInitialState: function() { return { liked: false }; }, handleClick: function(event) { this.setState({liked: !this.state.liked}); }, handleClick: function(event) { this.setState({liked: !this.state.liked}); }, render: function() { render: function() { var text = this.state.liked ? 'like' : ‘might like'; var text = this.state.liked ? 'like' : ‘might like'; return ( return ( You {text} this. Click to toggle. You {text} this. Click to toggle. ); ); }});ReactDOM.render(,, document.getElementById('example') document.getElementById('example'));

API getInitialState: function() getInitialState: function() –returns dictionary this.state this.state this.setState this.setState –merges key/values into state

Conclusions React JS unifies behavior and representation in single units called components React JS unifies behavior and representation in single units called components Code generation in JSX is exploited to provide a DSL for expressing elements of the UI Code generation in JSX is exploited to provide a DSL for expressing elements of the UI Pure JavaScript solution, without templating engine Pure JavaScript solution, without templating engine Programmer is in full control of the UI Programmer is in full control of the UI Components will eventually become part of browser capabilities Components will eventually become part of browser capabilities