2. Features of ReactJS
• Beta status
• component-based architecture
• loosely coupled components
• less code more application focus
• Supports ES6
3. What are Transpilers?
• They convert code from one language to another
• Babel (formerly 6to5):
+converts ES6 (some ES7) to ECMA5
+ appears to be the de facto standard
• Traceur (Google):
+ converts ES6 to ECMA5
+ used by Angular 2
4. Download/Install Code Samples
• 1) download/unzip Github repo (or clone repo)
• 2) npm install
• 3) double-click on the HTML Web page
5. React App (MyWebPage.html)
<!– 1) The core React library -->
<script src="https://fb.me/react-0.14.3.js"></script>
<!– 2) The ReactDOM Library -->
<script src="https://fb.me/react-dom-0.14.3.js"></script>
<!– 3) For transpiling ES6 code into ES5 code -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-
core/5.8.23/browser.min.js">
</script>
6. ReactJS “Hello World” (Version 1)
• <body>
• <div id="hello"></div>
•
• <script>
• ReactDOM.render(
• React.DOM.h1(null, "Hello World"),
• document.getElementById('hello'));
• </script>
• </body>
• NB: Babel.js is not required in this example
12. Property Types and Validation
• Throw an error if any property is negative:
• propTypes: {
• width: function(props, propName, componentName) {
• if(props[propName] < 0) {
• throw new Error(propName+" cannot be negative");
• }
• }
• },
• See: PropertyValidation.html
13. The “getDefaultProps()” Method
• <div id="container"></div>
• <script type="text/babel">
• var Hello = React.createClass({
• getDefaultProps: function () {
• return { y : 456 }
• },
• render: function () {
• return (
• <h2>x = {this.props.x} y = {this.props.y} </h2>
• );
• }
• });
ReactDOM.render(<Hello x={123}/>,
• document.getElementById('container'));
• </script>
14. Exercise: convert to ES6
• The previous example uses the deprecated syntax
• Convert the code to the “extend” style (used in 0.14.3)
• Hint: look at one of the previous “Hello World” examples
39. Exercises: List of Users via JSON Data
• 1a) Replace the array of names with this data:
• this.userList = [
• { name: 'Sara'},
• { name: 'Dave'},
• { name: 'John'},
• { name: 'Sally’} ];
• }
• 1b) update the code to reflect the modified data structure
• 2a) replace ‘name’ with ‘fname’
• 2b) also add an ‘lname’ property
• 3b) update the code to reflect the modified data structure
40. Working with JSON Data Files
• Read the contents of this text file and display the data:
• [
• {"fname":"Jane","lname":"Jones","city":"San Francisco"},
• {"fname":"John","lname":"Smith","city":"New York"},
• {"fname":"Dave","lname":"Stone","city":"Seattle"},
• {"fname":"Sara","lname":"Edson","city":"Chicago"}
• ]
45. Retrieving Github User Data (3)
• ReactDOM.render(
• <UserInfo
promise={$.getJSON('https://api.github.com/users/ocampesato')} />,
• document.getElementById("userinfo")
• );
46. What about ES6?
• Arrow functions and let keyword
• Block scopes
• Classes and inheritance
• Default parameters
• Destructured assignment
• Generators, Iterators, Maps, and Sets
• Promises and Rest parameters
• Spread operator
• Template Literals
47. ES6 let and Arrow Functions
• let square = x => x * x;
• let add = (x, y) => x + y;
• let pi = () => 3.1415;
• console.log(square(8)); // 64
• console.log(add(5, 9)); // 14
• console.log(pi()); // 3.1415
48. ES6 Class Definition (part 1)
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
calcArea() {
return this.height * this.width;
}
}
• var r1 = new Rectangle(5,10);
• var r2 = new Rectangle(25,15);
49. ES6 Class Definition (part 2)
• console.log("r1 area = "+r1.calcArea());
• console.log("r2 area = "+r2.calcArea());
• Test this code here:
http://babeljs.io/repl/
• More Examples:
https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Classes
50. Convert JSON Data to ES6 Class (1)
• Consider the following array of data:
var people = [
{fname:"Jane",lname:"Smith",zip:"94043"},
{fname:"John",lname:"Jones",zip:"94539"},
{fname:"Dave",lname:"Starr",zip:"67800"}
]
51. Convert JSON Data to ES6 Class (2)
class People {
• public fname:string;
• public lname:string;
• public zip:string;
• constructor(public fname:string,
• public lname:string,
• public zip:string) {
• this.fname = fname;
• this.lname = lname;
• this.zip = zip;
• }
}
52. Convert JSON Data to ES6 Class (3)
• Array of ES6 objects:
var ES6People = [
new People("Jane","Smith","94043"),
new People("John","Jones","94539"),
new People("Dave","Starr","67800")
];
53. Useful Tools/IDEs
• Select an IDE:
+WebStorm 10: free 30-day trial ($49/year)
+Visual Studio Code (free)
+ Atom (free) with atom-TypeScript extension
• Command Line Tools:
+ npm, npmjs, gulp, grunt (older), broccoli,
+ webpack, browserify (older), jspm+systemjs
https://github.com/addyosmani/es6-tools
54. Useful Technologies to Learn
• Main features of ES6
• Sass/Bootstrap 4 (previous: less)
• D3.js for Data Visualization
• React Native (=React for Mobile)
• https://egghead.io/react-redux-cheatsheets
55. Browser Status for ES6
• Modern IE: https://goo.gl/56n7IL
• Mozilla: https://goo.gl/iSNDf9
• Chrome: https://www.chromestatus.com/features#ES6
57. Recent/Upcoming Books and Training
1) HTML5 Canvas and CSS3 Graphics (2013)
2) jQuery, CSS3, and HTML5 for Mobile (2013)
3) HTML5 Pocket Primer (2013)
4) jQuery Pocket Primer (2013)
5) HTML5 Mobile Pocket Primer (2014)
6) D3 Pocket Primer (2015)
7) Python Pocket Primer (2015)
8) SVG Pocket Primer (2016)
9) CSS3 Pocket Primer (2016)
10) Angular Pocket Primer (2016)