SlideShare a Scribd company logo
React Development with the MERN Stack
Cowork South Bay 22 + 23 April 2017
Troy Miles
• Troy Miles aka the RocknCoder
• Over 38 years of programming
experience
• Speaker and author
• bit.ly/rc-jquerybook
• rockncoder@gmail.com
• @therockncoder
• Now a lynda.com Author!

React Development with the MERN Stack
Agenda
• Introduction
• MERN cli
• Node.js & Express
• MongoDB & Mongoose
• React
• Redux
• React-Router
• Ajax
• React-Bootstrap
• Summary
Tools
• install Git
• install Node.js
• upgrade npm npm install npm -g
• install the MERN CLI, npm install -g mern-cli
Versions
app command my version
git git —version 2.12.2
node.js node -v v7.9.0
npm npm —v 4.2.0
ECMAScript Versions
Version Date
ES1 June 1997
ES2 June 1998
ES3 December 1999
ES4 DOA 2006
ES5 December 2009
ES6/ES2015 June 2015
ES2016 2016
Array Operators
• .isArray()
• .every()
• .forEach()
• .indexOf()
• .lastIndexOf()
• .some()
• .map()
• .reduce()
• .filter()
forEach
let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];

let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

console.log(nums);



// forEach iterates over the array, once for each element, but there is no way to
// break out

nums.forEach(function (elem, index, arr) {

console.log(index + ': ' + elem);

});



map
let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];

let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

console.log(nums);



// map iterates over all of the elements and returns a new array with the same
// number of elements

let nums2 = nums.map((elem) => elem * 2);

console.log(nums2);

filter
let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];

let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

console.log(nums);



// filter iterates over the array and returns a new array with only the elements
// that pass the test

let nums3 = nums.filter((elem) => !!(elem % 2));

console.log(nums3);
reduce
let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];

let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

console.log(nums);



// reduce iterates over the array passing the previous value and the current

// element it is up to you what the reduction does, let's concatenate the strings

let letters2 = letters.reduce((previous, current) => previous + current);

console.log(letters2);



// reduceRight does the same but goes from right to left

let letters3 = letters.reduceRight((previous, current) => previous + current);

console.log(letters3);

every
let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];

let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

console.log(nums);



// every makes sure that all the elements match the expression

let isEveryNumbers = junk.every((elem) => typeof elem === 'number');

console.log('Are all members of junk numbers: ' + isEveryNumbers);



let
• let allows us to create a block scoped variables
• they live and die within their curly braces
• best practice is to use let instead of var
let
// let allows us to create block scoped variables

// they live and die within the curly braces

let val = 2;

console.info(`val = ${val}`);

{

let val = 59;

console.info(`val = ${val}`);

}

console.info(`val = ${val}`);



const
• const creates a variable that can't be changed
• best practice is to make any variable that should
not change a constant
• does not apply to object properties or array
elements
const
const name = 'Troy';

console.info(`My name is ${name}`);

// the line below triggers a type error

name = 'Miles';

Template strings
• Defined by using opening & closing back ticks
• Templates defined by ${JavaScript value}
• The value can be any simple JavaScript expression
• Allows multi-line strings (return is pass thru)
Template strings
let state = 'California';

let city = 'Long Beach';

console.info(`This weekend's workshop is in ${city}, ${state}.`);



// template strings can run simple expressions like addition

let cup_coffee = 4.5;

let cup_tea = 2.5;

console.info(`coffee: $${cup_coffee} + tea: $${cup_tea} = $$
{cup_coffee + cup_tea}.`);



// they can allow us to create multi-line strings

console.info(`This is line #1.

this is line #2.`);



Arrow functions
• Succinct syntax
• Doesn’t bind its own this, arguments, or super
• Facilitate a more functional style of coding
• Can’t be used as constructors
Arrow functions
• When only one parameter, parenthesis optional
• When zero or more than one parameter,
parenthesis required
Arrow function
let anon_func = function (num1, num2) {

return num1 + num2;

};

console.info(`Anonymous func: ${anon_func(1, 2)}`);



let arrow_func = (num1, num2) => num1 + num2;

console.info(`Arrow func: ${arrow_func(3, 4)}`);

this
• this is handle different in arrow functions
• In anonymous function this is bound to the global
object
• In arrow
• One important difference between anonymous and
arrow functions is
import
• Imports functions, objects, or primitives from other
files
• import <name> from “<module name>”;
Destructuring
Object Destructuring
16// this is a demo of the power of destructuring
17// we have two objects with the same 3 properties
18 const binary = {kb: 1024, mb: 1048576, gb: 1073741824};
19 const digital = {kb: 1000, mb: 1000000, gb: 1000000000};
20// We use a ternary statement to choose which object
21// assign properties based on their property names
22 const {kb, mb, gb} = (useBinary) ? binary : digital;
Spread syntax
• Expands an expression in places where multiple
arguments, elements, or variables are expected
Vital Definitions
• state
• mutation
• immutable
• pure functions
Application Root Directory
• All of the commands, for all of the tools are
designed work on the application root directory
• If used anywhere else bad things will happen
• be sure you are in the app root
• double check that you are in the app root
Webpack
Webpack
• Module bundler
• Replaces System.JS
• Works with JS, CSS, and HTML
• Minifies, concatenates, and bundles
How?
• Webpack starts at your app’s entry point
• It makes a graph of all of its dependencies
• It then bundles them together into an output file
Loaders
• Goal: Webpack handler loading of all of your app’s
assets
• Every file is a module
• Webpack only understands only JavaScript
• Loaders transform files into modules
Rules
• test: Identifies the file’s type
• use: Transform the file with a plugin loader
Example #1
module: {
rules: [
{
test: /.json$/,
loader: 'json-loader',
},
Example #2
module: {
rules: [
{
test: /.jsx*$/,
exclude: [/node_modules/, /.+.config.js/],
loader: 'babel-loader',
},
Example #3{
test: /.css$/,
use: [
{
loader: 'style-loader',
options: {
sourceMap: true
}
},
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
localIdentName: '[path]___[name]__[local]___[hash:base64:5]'
}
},
{
loader: 'postcss-loader'
}
],
},
Babel
Babel
• The compiler for writing the next generation
JavaScript
• current version 6.23.0
• works better with Webpack
Babel
• It is modular, with a small lightweight core
• Functionality comes from plugins
• All plugin are opt-in
Presets
• You might need a dozen plugins
• Keep track of them would be burdensome
• Presets are groups of related plugins
• Two popular ones are babel-preset-es2015 and
babel-preset-react
React
React
• A JavaScript library for building user interfaces
• Created by Facebook & Instagram
• Initial release March 2013
• Current version 15.4.2
• (Highly recommend reading their license)
React
• Virtual DOM
• One-way data flow
• JSX - JavaScript eXtension allows in HTML
generation
• Component-based
Component
• Fundamental building block of React
• Can be created with a JS Class or Function
React.PropTypes
• React.PropTypes is deprecated
• It will be deleted in React 16
• Use the npm package “prop-types” instead
• import	PropTypes	from	‘prop-types’;
Flux
• Application architecture for building user interfaces
• A pattern for managing data flow in your app
• One way data flow
• 4 main parts: Dispatcher, Store, Action, & View
The 4 main parts
• Dispatcher: receives actions & dispatches them to
stores
• Store: holds the data of an app
• Action: define app’s internal API
• View: displays the data from stores
React Development with the MERN Stack
Redux
Redux
• A predictable state container for JS apps
• Works well with React Native
• An alternative to & inspired by Flux
• Single store for the entire app
• Makes it easier to hot-load your app
• Created by Dan Abramov
3 Fundamental Principals
• Single Source of Truth
• State is Read-Only
• Changes are Made with Pure Functions
React-Redux
• Provides binds to React bindings to redux
• npm	i	-S	react-redux	
• Separates presentational and container
components
Container Components
• A React component which uses store.subscribe
• Could be created by hand, but easier using
connect()
• All containers need to access to the Redux store
• React-Redux includes <Provider> component for
store access
React Router
• A complete routing library for React
• Keeps UI in sync with URL
Node.js
Don’t use sudo
• Using sudo with npm is not a best practice
• sudo chown -R $USER /usr/local
• The command makes you the owner of your local
directory
• That enables apps your in that directory like npm,

able to work without more permissions
Node 7
• New version of Chrome V8
• Supports ES6
• Faster
• node -v
Express
Express
• Web framework for Node.js
• Like Rails is to Ruby
• We will use for routing and first page
mLab
https://mlab.com/
mLab
• Sign up for the Sandbox plan
• It comes with 0.5 GB of space
• Support is included
Connection Info
• From the home page
• Click on your db
• In the upper left is your info
• Copy the shell connect info
MongoDB
Who Uses MongoDB
• Craigslist
• eBay
• Foursquare
• SourceForge
• Viacom
• Expedia
• Parse
• LinkedIn
• Medtronic
• eHarmony
• CERN
• and more
Features
• Document Database
• High Performance
• High Availability
• Easy Scalability
• Geospatial Data
Document
• An ordered set of keys and values
• like JavaScript objects
• no duplicate keys allowed
• type and case sensitive
• field order is not important nor guaranteed
Mongo Shell Remotely
• mongo	ds045054.mlab.com:45054/quiz	-u	<dbuser>	-p	<dbpassword>	
• <dbuser>	is	your	database	user	name	
• <dbpassword>	is	your	database	password
Shell Commands
• show dbs
• use <database name>
• show collections
• db.<collection name>.drop()
Queries
• db.<collection name>.find(<query>)
• skip()
• limit()
• sort()
• pretty()
Insert & Delete Data
• insert()
• remove(<query>)
Environment Variables
• process.env object holds environment vars
• Reading: var dbConnect =
process.env.dbConnect;
• Writing: process.env.mode = ‘test’;
Heroku Environment Vars
• Reading - heroku config
• heroku config:get var-name
• Writing - heroku config:set var=value
• heroku config:unset var-name
Setting Environment Vars
• http://bit.ly/rc-setenv
• Mac terminal - export variable=value
Mongoose
Mongoose
• Object Data Modeling (ODM)
• Mongoose makes MongoDB easier to work with
• Makes it easier to connect and to work with objects
Mongoose
• npm install mongoose —save
• var mongoose = require(‘mongoose’);
• mongoose.connect(<connection string>);
Mongoose Schema Types
• Array
• Boolean
• Buffer
• Date
• Mixed
• Number
• ObjectId
• String
Links
• https://git-scm.com/downloads
• https://nodejs.org/en/
• http://expressjs.com/
• https://www.heroku.com/
• https://mlab.com/
Links
• https://facebook.github.io/react/
• http://redux.js.org/
• https://reacttraining.com/react-router/
Summary
• React is good
• Redux makes it better
• Combine with Node & MongoDB to create
lightweight Web Apps

More Related Content

PDF
MEAN Stack
Krishnaprasad k
 
PDF
Basics of JavaScript
Bala Narayanan
 
PPT
Java Script ppt
Priya Goyal
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPT
Spring Boot in Action
Alex Movila
 
PDF
Javascript basics
shreesenthil
 
PPTX
Typescript ppt
akhilsreyas
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
MEAN Stack
Krishnaprasad k
 
Basics of JavaScript
Bala Narayanan
 
Java Script ppt
Priya Goyal
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Spring Boot in Action
Alex Movila
 
Javascript basics
shreesenthil
 
Typescript ppt
akhilsreyas
 
Java Server Pages(jsp)
Manisha Keim
 

What's hot (20)

PPTX
Introduction to Spring Boot
Purbarun Chakrabarti
 
PPTX
Laravel ppt
Mayank Panchal
 
PPTX
Reactjs
Neha Sharma
 
PPT
Javascript arrays
Hassan Dar
 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PDF
JavaScript Programming
Sehwan Noh
 
PDF
NodeJS for Beginner
Apaichon Punopas
 
PDF
Introduction to ASP.NET Core
Avanade Nederland
 
PDF
Introduction to web development
Mohammed Safwat
 
PPTX
Spring boot
Pradeep Shanmugam
 
PPT
Maven Introduction
Sandeep Chawla
 
PPTX
Hibernate ppt
Aneega
 
PPT
Java tutorial PPT
Intelligo Technologies
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPTX
JSON: The Basics
Jeff Fox
 
PPT
ASP.NET MVC Presentation
ivpol
 
PPTX
Express js
Manav Prasad
 
PDF
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
PPTX
ASP.NET Web API
habib_786
 
Introduction to Spring Boot
Purbarun Chakrabarti
 
Laravel ppt
Mayank Panchal
 
Reactjs
Neha Sharma
 
Javascript arrays
Hassan Dar
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
JavaScript Programming
Sehwan Noh
 
NodeJS for Beginner
Apaichon Punopas
 
Introduction to ASP.NET Core
Avanade Nederland
 
Introduction to web development
Mohammed Safwat
 
Spring boot
Pradeep Shanmugam
 
Maven Introduction
Sandeep Chawla
 
Hibernate ppt
Aneega
 
Java tutorial PPT
Intelligo Technologies
 
Introduction to Javascript
Amit Tyagi
 
JSON: The Basics
Jeff Fox
 
ASP.NET MVC Presentation
ivpol
 
Express js
Manav Prasad
 
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
ASP.NET Web API
habib_786
 
Ad

Similar to React Development with the MERN Stack (20)

PDF
Intro to React
Troy Miles
 
PDF
React Native Evening
Troy Miles
 
PDF
Angular Weekend
Troy Miles
 
PDF
React Native One Day
Troy Miles
 
PDF
Node Boot Camp
Troy Miles
 
PPTX
React_Complete.pptx
kamalakantas
 
PDF
The Road To Redux
Jeffrey Sanchez
 
PDF
Using React, Redux and Saga with Lottoland APIs
Mihail Gaberov
 
PDF
React, Redux, ES2015 by Max Petruck
Maksym Petruk
 
PDF
React, Redux, ES2015 by Max Petruck
Lingvokot
 
PPTX
ES6 and BEYOND
Brian Patterson
 
PDF
Lezione 03 Introduzione a react
University of Catania
 
PPTX
Getting started with ES6
Nitay Neeman
 
PDF
TDC2016SP - Trilha Frameworks JavaScript
tdc-globalcode
 
PDF
React for Beginners
Derek Willian Stavis
 
PDF
Getting Started with React, When You’re an Angular Developer
Fabrit Global
 
PDF
react hook and wesite making structure ppt
arunkumarn100
 
PPTX
React JS: A Secret Preview
valuebound
 
PPTX
React.js - The Dawn of Virtual DOM
Jimit Shah
 
PPTX
React october2017
David Greenfield
 
Intro to React
Troy Miles
 
React Native Evening
Troy Miles
 
Angular Weekend
Troy Miles
 
React Native One Day
Troy Miles
 
Node Boot Camp
Troy Miles
 
React_Complete.pptx
kamalakantas
 
The Road To Redux
Jeffrey Sanchez
 
Using React, Redux and Saga with Lottoland APIs
Mihail Gaberov
 
React, Redux, ES2015 by Max Petruck
Maksym Petruk
 
React, Redux, ES2015 by Max Petruck
Lingvokot
 
ES6 and BEYOND
Brian Patterson
 
Lezione 03 Introduzione a react
University of Catania
 
Getting started with ES6
Nitay Neeman
 
TDC2016SP - Trilha Frameworks JavaScript
tdc-globalcode
 
React for Beginners
Derek Willian Stavis
 
Getting Started with React, When You’re an Angular Developer
Fabrit Global
 
react hook and wesite making structure ppt
arunkumarn100
 
React JS: A Secret Preview
valuebound
 
React.js - The Dawn of Virtual DOM
Jimit Shah
 
React october2017
David Greenfield
 
Ad

More from Troy Miles (20)

PDF
Fast C++ Web Servers
Troy Miles
 
PDF
AWS Lambda Function with Kotlin
Troy Miles
 
PDF
Angular Application Testing
Troy Miles
 
PDF
ReactJS.NET
Troy Miles
 
PDF
What is Angular version 4?
Troy Miles
 
PDF
From MEAN to the MERN Stack
Troy Miles
 
PDF
Functional Programming in JavaScript
Troy Miles
 
PDF
Functional Programming in Clojure
Troy Miles
 
PDF
MEAN Stack Warm-up
Troy Miles
 
PDF
The JavaScript You Wished You Knew
Troy Miles
 
PDF
Game Design and Development Workshop Day 1
Troy Miles
 
PDF
Build a Game in 60 minutes
Troy Miles
 
PDF
Quick & Dirty & MEAN
Troy Miles
 
PDF
A Quick Intro to ReactiveX
Troy Miles
 
PDF
JavaScript Foundations Day1
Troy Miles
 
PDF
AngularJS Beginner Day One
Troy Miles
 
PDF
AngularJS on Mobile with the Ionic Framework
Troy Miles
 
PDF
Building Cross-Platform Mobile Apps
Troy Miles
 
PDF
Cross Platform Game Programming with Cocos2d-js
Troy Miles
 
PDF
10 Groovy Little JavaScript Tips
Troy Miles
 
Fast C++ Web Servers
Troy Miles
 
AWS Lambda Function with Kotlin
Troy Miles
 
Angular Application Testing
Troy Miles
 
ReactJS.NET
Troy Miles
 
What is Angular version 4?
Troy Miles
 
From MEAN to the MERN Stack
Troy Miles
 
Functional Programming in JavaScript
Troy Miles
 
Functional Programming in Clojure
Troy Miles
 
MEAN Stack Warm-up
Troy Miles
 
The JavaScript You Wished You Knew
Troy Miles
 
Game Design and Development Workshop Day 1
Troy Miles
 
Build a Game in 60 minutes
Troy Miles
 
Quick & Dirty & MEAN
Troy Miles
 
A Quick Intro to ReactiveX
Troy Miles
 
JavaScript Foundations Day1
Troy Miles
 
AngularJS Beginner Day One
Troy Miles
 
AngularJS on Mobile with the Ionic Framework
Troy Miles
 
Building Cross-Platform Mobile Apps
Troy Miles
 
Cross Platform Game Programming with Cocos2d-js
Troy Miles
 
10 Groovy Little JavaScript Tips
Troy Miles
 

Recently uploaded (20)

PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
PDF
Winning Business in a Slowing Economy, How CPQ helps Manufacturers Protect Ma...
systemscincom
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
introduction to dart --- Section one .pptx
marknaiem92
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Save Business Costs with CRM Software for Insurance Agents
Insurance Tech Services
 
PPTX
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
PPT
FALLSEM2025-26_ISWE304L_TH_VL2025260102786_2025-07-10_Reference-Material-II.ppt
AKSHAYA255427
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
PPTX
Benefits of DCCM for Genesys Contact Center
pointel ivr
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Exploring AI Agents in Process Industries
amoreira6
 
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
Winning Business in a Slowing Economy, How CPQ helps Manufacturers Protect Ma...
systemscincom
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
introduction to dart --- Section one .pptx
marknaiem92
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Save Business Costs with CRM Software for Insurance Agents
Insurance Tech Services
 
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
FALLSEM2025-26_ISWE304L_TH_VL2025260102786_2025-07-10_Reference-Material-II.ppt
AKSHAYA255427
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
Benefits of DCCM for Genesys Contact Center
pointel ivr
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 

React Development with the MERN Stack

  • 1. React Development with the MERN Stack Cowork South Bay 22 + 23 April 2017
  • 2. Troy Miles • Troy Miles aka the RocknCoder • Over 38 years of programming experience • Speaker and author • bit.ly/rc-jquerybook • [email protected] • @therockncoder • Now a lynda.com Author!

  • 4. Agenda • Introduction • MERN cli • Node.js & Express • MongoDB & Mongoose • React • Redux • React-Router • Ajax • React-Bootstrap • Summary
  • 5. Tools • install Git • install Node.js • upgrade npm npm install npm -g • install the MERN CLI, npm install -g mern-cli
  • 6. Versions app command my version git git —version 2.12.2 node.js node -v v7.9.0 npm npm —v 4.2.0
  • 7. ECMAScript Versions Version Date ES1 June 1997 ES2 June 1998 ES3 December 1999 ES4 DOA 2006 ES5 December 2009 ES6/ES2015 June 2015 ES2016 2016
  • 8. Array Operators • .isArray() • .every() • .forEach() • .indexOf() • .lastIndexOf() • .some() • .map() • .reduce() • .filter()
  • 9. forEach let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];
 let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
 let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
 console.log(nums);
 
 // forEach iterates over the array, once for each element, but there is no way to // break out
 nums.forEach(function (elem, index, arr) {
 console.log(index + ': ' + elem);
 });
 

  • 10. map let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];
 let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
 let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
 console.log(nums);
 
 // map iterates over all of the elements and returns a new array with the same // number of elements
 let nums2 = nums.map((elem) => elem * 2);
 console.log(nums2);

  • 11. filter let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];
 let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
 let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
 console.log(nums);
 
 // filter iterates over the array and returns a new array with only the elements // that pass the test
 let nums3 = nums.filter((elem) => !!(elem % 2));
 console.log(nums3);
  • 12. reduce let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];
 let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
 let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
 console.log(nums);
 
 // reduce iterates over the array passing the previous value and the current
 // element it is up to you what the reduction does, let's concatenate the strings
 let letters2 = letters.reduce((previous, current) => previous + current);
 console.log(letters2);
 
 // reduceRight does the same but goes from right to left
 let letters3 = letters.reduceRight((previous, current) => previous + current);
 console.log(letters3);

  • 13. every let junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];
 let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
 let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
 console.log(nums);
 
 // every makes sure that all the elements match the expression
 let isEveryNumbers = junk.every((elem) => typeof elem === 'number');
 console.log('Are all members of junk numbers: ' + isEveryNumbers);
 

  • 14. let • let allows us to create a block scoped variables • they live and die within their curly braces • best practice is to use let instead of var
  • 15. let // let allows us to create block scoped variables
 // they live and die within the curly braces
 let val = 2;
 console.info(`val = ${val}`);
 {
 let val = 59;
 console.info(`val = ${val}`);
 }
 console.info(`val = ${val}`);
 

  • 16. const • const creates a variable that can't be changed • best practice is to make any variable that should not change a constant • does not apply to object properties or array elements
  • 17. const const name = 'Troy';
 console.info(`My name is ${name}`);
 // the line below triggers a type error
 name = 'Miles';

  • 18. Template strings • Defined by using opening & closing back ticks • Templates defined by ${JavaScript value} • The value can be any simple JavaScript expression • Allows multi-line strings (return is pass thru)
  • 19. Template strings let state = 'California';
 let city = 'Long Beach';
 console.info(`This weekend's workshop is in ${city}, ${state}.`);
 
 // template strings can run simple expressions like addition
 let cup_coffee = 4.5;
 let cup_tea = 2.5;
 console.info(`coffee: $${cup_coffee} + tea: $${cup_tea} = $$ {cup_coffee + cup_tea}.`);
 
 // they can allow us to create multi-line strings
 console.info(`This is line #1.
 this is line #2.`);
 

  • 20. Arrow functions • Succinct syntax • Doesn’t bind its own this, arguments, or super • Facilitate a more functional style of coding • Can’t be used as constructors
  • 21. Arrow functions • When only one parameter, parenthesis optional • When zero or more than one parameter, parenthesis required
  • 22. Arrow function let anon_func = function (num1, num2) {
 return num1 + num2;
 };
 console.info(`Anonymous func: ${anon_func(1, 2)}`);
 
 let arrow_func = (num1, num2) => num1 + num2;
 console.info(`Arrow func: ${arrow_func(3, 4)}`);

  • 23. this • this is handle different in arrow functions • In anonymous function this is bound to the global object • In arrow • One important difference between anonymous and arrow functions is
  • 24. import • Imports functions, objects, or primitives from other files • import <name> from “<module name>”;
  • 26. Object Destructuring 16// this is a demo of the power of destructuring 17// we have two objects with the same 3 properties 18 const binary = {kb: 1024, mb: 1048576, gb: 1073741824}; 19 const digital = {kb: 1000, mb: 1000000, gb: 1000000000}; 20// We use a ternary statement to choose which object 21// assign properties based on their property names 22 const {kb, mb, gb} = (useBinary) ? binary : digital;
  • 27. Spread syntax • Expands an expression in places where multiple arguments, elements, or variables are expected
  • 28. Vital Definitions • state • mutation • immutable • pure functions
  • 29. Application Root Directory • All of the commands, for all of the tools are designed work on the application root directory • If used anywhere else bad things will happen • be sure you are in the app root • double check that you are in the app root
  • 31. Webpack • Module bundler • Replaces System.JS • Works with JS, CSS, and HTML • Minifies, concatenates, and bundles
  • 32. How? • Webpack starts at your app’s entry point • It makes a graph of all of its dependencies • It then bundles them together into an output file
  • 33. Loaders • Goal: Webpack handler loading of all of your app’s assets • Every file is a module • Webpack only understands only JavaScript • Loaders transform files into modules
  • 34. Rules • test: Identifies the file’s type • use: Transform the file with a plugin loader
  • 35. Example #1 module: { rules: [ { test: /.json$/, loader: 'json-loader', },
  • 36. Example #2 module: { rules: [ { test: /.jsx*$/, exclude: [/node_modules/, /.+.config.js/], loader: 'babel-loader', },
  • 37. Example #3{ test: /.css$/, use: [ { loader: 'style-loader', options: { sourceMap: true } }, { loader: 'css-loader', options: { modules: true, importLoaders: 1, localIdentName: '[path]___[name]__[local]___[hash:base64:5]' } }, { loader: 'postcss-loader' } ], },
  • 38. Babel
  • 39. Babel • The compiler for writing the next generation JavaScript • current version 6.23.0 • works better with Webpack
  • 40. Babel • It is modular, with a small lightweight core • Functionality comes from plugins • All plugin are opt-in
  • 41. Presets • You might need a dozen plugins • Keep track of them would be burdensome • Presets are groups of related plugins • Two popular ones are babel-preset-es2015 and babel-preset-react
  • 42. React
  • 43. React • A JavaScript library for building user interfaces • Created by Facebook & Instagram • Initial release March 2013 • Current version 15.4.2 • (Highly recommend reading their license)
  • 44. React • Virtual DOM • One-way data flow • JSX - JavaScript eXtension allows in HTML generation • Component-based
  • 45. Component • Fundamental building block of React • Can be created with a JS Class or Function
  • 46. React.PropTypes • React.PropTypes is deprecated • It will be deleted in React 16 • Use the npm package “prop-types” instead • import PropTypes from ‘prop-types’;
  • 47. Flux • Application architecture for building user interfaces • A pattern for managing data flow in your app • One way data flow • 4 main parts: Dispatcher, Store, Action, & View
  • 48. The 4 main parts • Dispatcher: receives actions & dispatches them to stores • Store: holds the data of an app • Action: define app’s internal API • View: displays the data from stores
  • 50. Redux
  • 51. Redux • A predictable state container for JS apps • Works well with React Native • An alternative to & inspired by Flux • Single store for the entire app • Makes it easier to hot-load your app • Created by Dan Abramov
  • 52. 3 Fundamental Principals • Single Source of Truth • State is Read-Only • Changes are Made with Pure Functions
  • 53. React-Redux • Provides binds to React bindings to redux • npm i -S react-redux • Separates presentational and container components
  • 54. Container Components • A React component which uses store.subscribe • Could be created by hand, but easier using connect() • All containers need to access to the Redux store • React-Redux includes <Provider> component for store access
  • 55. React Router • A complete routing library for React • Keeps UI in sync with URL
  • 57. Don’t use sudo • Using sudo with npm is not a best practice • sudo chown -R $USER /usr/local • The command makes you the owner of your local directory • That enables apps your in that directory like npm,
 able to work without more permissions
  • 58. Node 7 • New version of Chrome V8 • Supports ES6 • Faster • node -v
  • 60. Express • Web framework for Node.js • Like Rails is to Ruby • We will use for routing and first page
  • 62. mLab • Sign up for the Sandbox plan • It comes with 0.5 GB of space • Support is included
  • 63. Connection Info • From the home page • Click on your db • In the upper left is your info • Copy the shell connect info
  • 65. Who Uses MongoDB • Craigslist • eBay • Foursquare • SourceForge • Viacom • Expedia • Parse • LinkedIn • Medtronic • eHarmony • CERN • and more
  • 66. Features • Document Database • High Performance • High Availability • Easy Scalability • Geospatial Data
  • 67. Document • An ordered set of keys and values • like JavaScript objects • no duplicate keys allowed • type and case sensitive • field order is not important nor guaranteed
  • 68. Mongo Shell Remotely • mongo ds045054.mlab.com:45054/quiz -u <dbuser> -p <dbpassword> • <dbuser> is your database user name • <dbpassword> is your database password
  • 69. Shell Commands • show dbs • use <database name> • show collections • db.<collection name>.drop()
  • 70. Queries • db.<collection name>.find(<query>) • skip() • limit() • sort() • pretty()
  • 71. Insert & Delete Data • insert() • remove(<query>)
  • 72. Environment Variables • process.env object holds environment vars • Reading: var dbConnect = process.env.dbConnect; • Writing: process.env.mode = ‘test’;
  • 73. Heroku Environment Vars • Reading - heroku config • heroku config:get var-name • Writing - heroku config:set var=value • heroku config:unset var-name
  • 74. Setting Environment Vars • http://bit.ly/rc-setenv • Mac terminal - export variable=value
  • 76. Mongoose • Object Data Modeling (ODM) • Mongoose makes MongoDB easier to work with • Makes it easier to connect and to work with objects
  • 77. Mongoose • npm install mongoose —save • var mongoose = require(‘mongoose’); • mongoose.connect(<connection string>);
  • 78. Mongoose Schema Types • Array • Boolean • Buffer • Date • Mixed • Number • ObjectId • String
  • 79. Links • https://git-scm.com/downloads • https://nodejs.org/en/ • http://expressjs.com/ • https://www.heroku.com/ • https://mlab.com/
  • 81. Summary • React is good • Redux makes it better • Combine with Node & MongoDB to create lightweight Web Apps