React From Scratch - Software Design & Development...

275
@housecor | bitnative.com Cory House React From Scratch github.com/coryhouse/fluent2016 Go here and get setup! Finished demo: github.com/coryhouse/fluent2016complete

Transcript of React From Scratch - Software Design & Development...

Page 1: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

@housecor | bitnative.com

Cory House

React From Scratch

github.com/coryhouse/fluent2016Go here and get setup!

Finished demo: github.com/coryhouse/fluent2016complete

Page 2: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Plan to code along with me?

Focus: Code or concepts?

Atom or Webstorm?

Light or dark theme?

Breaks?

Questions:

Page 3: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

To code along with me:

git clone https://github.com/coryhouse/fluent2016.git

Page 4: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Start github.com/coryhouse/fluent2016

Finish github.com/coryhouse/fluent2016complete

Code snippets bit.ly/fluent2016reactplan

Page 5: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

A World of Components

Page 6: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

They call it React for a reason.

Page 7: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 8: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 9: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 10: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 11: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 12: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

JSX

Virtual DOM

Isomorphic Rendering

Unidirectional Flows

Innovation

Page 13: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Try to keep an open mind.

Page 14: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

HTML should be a projection of app state, not a source of truth.

Page 15: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

JavaScript and HTML belong in the same file.

Page 16: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Unidirectional flow. No two-way binding.

Page 17: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Inline styles can be good.

Page 18: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

All Variables Should be Global.

Page 19: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Battle Proven

Page 20: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Morning:

React

Afternoon:

React exercises

Development environment

Testing

Redux

Here’s the Plan

Page 21: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Environment Setup

Page 22: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Everyone done this?

github.com/coryhouse/fluent2016

Page 23: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

You don’t need all this tech to work with

React and Flux.

Don’t Let This Scare You…

Page 24: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Just type “npm start”, get all this…

▪ Compile React JSX

▪ Lint JSX and JS via ESLint

▪ Bundle JS and CSS files

▪ Migrate the built app to the dist folder

▪ Run a dev webserver

▪ Open the browser at your dev URL

▪ Reload the browser upon save

Our Goal

Page 25: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Core Technologies

Server-side JavaScript Components

Unidirectional data

flows

Task runner

Node packages in the browser

Routing

Page 26: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Server-side JS

Uses the V8 Engine

Includes npm package manager

Node

Page 27: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

.NET

Ruby

Java

Python

PHP

The Web

Node

npm: Node’s Package Manager

Page 28: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

CommonJS Pattern

//1. Get reference to dependencyvar dependency = require('/path/to/file');

//2. Declare modulevar MyModule = {//code here…

}

//3. Expose to othersmodule.exports = MyModule;

Page 29: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Use Node modules in the browser

Bundle dependencies

Webpack

Page 30: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Component Library

Simple composition

Utilizes virtual DOM

Can render on client and server

React

Page 31: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Uni-directional data flows

Redux

Page 32: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Run tasks

Rich plugin ecosystem

Stream-based

npm Scripts

Page 33: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Approved June 2015

Widespread browser support

Can transpile to ES5 via Babel

ES2015 (ES6)

Approved in 2009

The JS you already know

Excellent browser support

ES5

What about ES2015?

Page 34: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

What about ES2015?

Page 35: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Node & npm Packages

React Components

Redux Data flows

Webpack Bundler

Npm Scripts Automationandbuilds

Wrap Up

Page 36: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

JSX

Page 37: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This baby is clearly

adorable.

Imagine an ugly baby here.

Page 38: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 39: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 40: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

“HTML” in JavaScript

Compiles to JavaScript

Differences: className, htmlFor

Optional

JSX

Page 41: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React.createElement("h1", {color: "red"}, "Heading here")

<h1 color="red">Heading here</h1>

JSX Compiles to JS

Page 42: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 43: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

What about separation of concerns?

Page 44: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

“JS” in HTML

<div ng-repeat="user in users">

{{#each user in users}}

data-bind="foreach: users">

“HTML” in JS

{users.map(createUserRow)}

Page 45: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 46: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Must stay in sync.

No explicit interface!

Page 47: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Separating intertwined concerns hinders debugging

Page 48: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Compile-time error checking FTW!

Page 49: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 50: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

M V C

Page 51: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

M V C

Page 52: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

JSX Friendly Editors

Page 53: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Virtual DOM

Page 54: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 55: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Updating the DOM is expensive

Why Virtual DOM?

Page 56: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Compare DOM’s current state to

desired new state.

Update the DOM in the most

efficient way.

With Virtual DOM

Blindly update DOM using new

state.

Without Virtual DOM

The Virtual DOM

Page 57: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Removing a Row…

Redraws entire table Removes the row

Page 58: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Hard to argue with the results…

Page 59: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Performance

Page 60: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Performance

Page 61: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 62: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

shouldComponentUpdate

PureRenderMixin + immutability

Performance: Go Even Faster

Page 63: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Meh, my app is fast enough.

Page 64: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Half second delay -> 20% drop in traffic.

Delayed in increments of 100

milliseconds

Even very small delays resulted in

substantial drops in revenue.

Page 65: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 66: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Synthetic Events

Isomorphic Support

React Native

Hot Reloading

Virtual DOM: More Than Performance

Page 67: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Let’s code our first components!

Page 68: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Time to start coding!

▪ Create our first React components

▪ Setup custom routing

▪ Create centralized layout

▪ Discuss naming conventions

Here’s the Plan

Page 69: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

chatApp.jsChatApp.jsxChatApp.react.js

Naming React Files

Page 70: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

.js .jsx

All IDEs will at least highlight JS

Default application/icon

Require statements assume JS

Doesn’t distinguish JSX from JS

Lies about contents

May confuse IDE highlighting

Some IDEs won’t highlight any syntax

No default application/icon

Must use .jsx extension to require

Distinguishes JS from JSX

Clarifies content

IDE knows to parse & color as JSX

Which Extension?

Page 71: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React Component Approaches

Page 72: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ React component creation approaches

▪ Container vs Presentational Components

Here’s the Plan

Page 73: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Four+ Ways to Create React Components?!

Page 74: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ ES5 createClass

▪ ES6 class

▪ ES5 stateless function

▪ ES6 stateless function

▪ Many more…

Ways to Create Components

Page 75: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

var HelloWorld = React.createClass({

render: function () {

return (

<h1>Hello World</h1>

);

}

});

ES5 Class Component

Page 76: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

class HelloWorld extends React.Component {

constructor(props) {

super(props);

}

render() {

return (

<h1>Hello World</h1>

);

}

}

ES6 Class Component

Page 77: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ No autobind

▪ PropTypes declared separately

▪ Default props declared separately

▪ Set initial state in constructor

React in ES6

Page 78: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

var HelloWorld = function(props) {

return (

<h1>Hello World</h1>

);

});

ES5 Stateless Functional Component

Page 79: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

const HelloWorld = (props) => {

return (

<h1>Hello World</h1>

);

});

ES6 Stateless Functional Component

Page 80: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 81: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ No class needed

▪ Avoid `this` keyword

▪ Enforced best practices

▪ High signal-to-noise ratio

▪ Enhanced code completion / intellisense

▪ Bloated components are obvious

▪ Easy to understand

▪ Easy to test

▪ Performance

Stateless Functional Components: 9 Benefits

Page 82: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Use stateless functional components when possible.

Page 83: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Class Component

State

Refs

Lifecycle methods

Child functions (for performance)

▪ Stateless Components

Everywhere else ☺

When Should I Use Each?

Page 84: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Object.create

▪ Mixins

▪ Parasitic Components

▪ StampIt

More info: bit.ly/react-define-component

Other Ways to Create Components

Page 85: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Container vs Presentation Components

Page 86: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Container

Little to no markup

Pass data and actions down

Knows about Redux

Often stateful

▪ Presentation

Nearly all markup

Receive data and actions via props

Doesn’t know about Redux

Typically functional components

Most components

Page 87: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Container

Smart

Stateful

Controller View

Presentational

Dumb

Stateless

View

Alternative Jargon

Page 88: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

”When you notice that some components

don’t use props they receive but merely

forward them down…it’s a good time to

introduce some container components.”

Dan Abramov

Page 89: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ ES5 createClass

▪ ES6 Class

▪ ES5 Stateless Function

▪ ES6 Stateless Function

▪ Many more!

▪ Container vs Presentation Components

▪ Next up: Let’s start building!

Wrap up

Page 90: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React Lifecycle

Page 91: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Props

▪ State

▪ Dynamic child components

▪ Lifecycle functions

▪ Demo time!

- Mock API

- Dynamic components

Here’s the Plan

Page 92: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Props – Look like HTML attributes, but immutable

this.props.username

State – Holds mutable state

this.state.username

Props and State

Page 93: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ getInitialState

Initial State

Page 94: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ getDefaultProps

Default Prop Values

Page 95: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

componentWillMount

componentDidMount

componentWillReceiveProps

shouldComponentUpdate

componentWillUpdate

componentDidUpdate

componentWillUnmount

Lifecycle Methods

Page 96: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

When

Before initial render, both client and server

Why

Good spot to set initial state

componentWillMount

Page 97: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

When

After render

Why

Access DOM, integrate with frameworks, set timers, AJAX requests

componentDidMount

Page 98: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

When

When receiving new props. Not called on initial render.

Why

Set state before a render.

componentWillReceiveProps

Page 99: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

When

Before render when new props or state are being received.

Not called on initial render.

Why

Performance. Return false to void unnecessary re-renders.

shouldComponentUpdate

Page 100: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

When

Immediately before rendering when new props or state are being received.

Not called on initial render.

Why

Prepare for an update

componentWillUpdate

Page 101: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

When

After component's updates are flushed to the DOM.

Not called for the initial render.

Why

Work with the DOM after an update

componentDidUpdate

Page 102: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

When

Immediately before component is removed from the DOM

Why

Cleanup

componentWillUnmount

Page 103: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Add a key to dynamic child elements

<tr key={author.id}>

Keys for Dynamic Children

Page 104: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Props Pass data to child components

State Data in controller view

Lifecycle Handle bootstrapping and third party integrations

Summary

Page 105: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Composing Components

Page 106: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Our focus: Composition

Controller views

Prop validation via PropTypes

Mixins

Here’s the Plan

Page 107: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Composition

Page 108: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Interacts with storesSets props on

childrenTop level component

Controller Views

Page 109: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Controller Views

Page 110: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

propTypes: {

author: React.PropTypes.object.isRequired,

onSave: React.PropTypes.func.isRequired,

validate: React.PropTypes.func.isRequired,

errors: React.PropTypes.object,

hasErrors: React.PropTypes.func.isRequired,

},

Prop Validation

Page 111: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

optionalArray: React.PropTypes.array,

optionalBool: React.PropTypes.bool,

optionalFunc: React.PropTypes.func,

optionalNumber: React.PropTypes.number,

optionalObject: React.PropTypes.object,

optionalString: React.PropTypes.string,

Prop Validation

Page 112: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Minified version is for production.

Development vs Production Mode

Page 113: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Declare expected property types

Prop Validation

Page 114: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Controller Views “Smart” components that pass data down via props

PropTypes Validate props

Summary

Page 115: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

@housecor bitnative.com

Cory House

Testing and Continuous Integration

Page 116: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ JavaScript testing styles

▪ 6 key testing decisions

▪ Configure testing and write test

▪ Continuous integration

Here’s the Plan

Page 117: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

JavaScript Testing Styles

Style Focus

Unit Single function or module

Integration Interactions between modules

UI Automate interactions with UI

Page 118: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

3▪ Helper Libraries

6▪ Where to place tests

1Framework

2▪ Assertion Library

4▪ Where to run tests

5▪ When to run tests

Unit Testing Decisions

Page 119: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Decision #1:Testing Framework

Page 120: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Tape

▪ AVA

Mocha ▪ Jasmine

▪ QUnit ▪ Jest

Testing Frameworks

Page 121: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

It’s Like Choosing a Gym

The important part is showing up.

Page 122: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Right Answer Wrong Answer

Any of these!

Woah, decision fatigue!

I’ll just keep coding

and praying.

Page 123: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Decision #2:Assertion Library

Page 124: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Declare what you expect

What’s An Assertion?

expect(2+2).to.equal(4)

assert(2+2).equals(4)

Page 125: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Assertion Libraries

Page 126: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Decision #3:Helper Library

Page 127: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Simulate the browser’s DOM

▪ Run DOM-related tests without a browser

JSDOM

Page 128: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ jQuery for the server

▪ Query virtual DOM using jQuery selectors

Cheerio

Page 129: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Decision #4:Where to Run Tests

Page 130: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Browser

- Karma, Testem

▪ Headless Browser

- PhantomJS

▪ In-memory DOM

- JSDOM

Where to Run Tests

Page 131: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Decision #5:Where do test files belong?

Page 132: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Centralized

Less “noise” in src folder

Deployment confusion

Inertia

Alongside

Easy imports

Clear visibility

Convenient to open

No recreating folder structure

Easy file moves

Where Do Test Files Belong?

// file.test.js

import file from '../../src/long/path'

// file.test.js

import file from './file'

Path to file under test is always ./filename ☺

Page 133: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Naming Test Files

Page 134: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Decision #6:When should tests run?

Page 135: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Rapid feedback

▪ Facilitates TDD

▪ Automatic = Low friction

▪ Increases test visibility

Unit Tests Should Run When You Hit Save

Page 136: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

But Cory, my tests are

too slow!

- You, my viewer with slow tests

Page 137: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Unit Tests

Test a small unit

Often single function

Fast

Run upon save

▪ Integration Tests

Test multiple units

Often involves clicking and waiting

Slow

Often run on demand, or in QA

Page 138: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

3▪ Helper Libraries

6▪ Where to place tests

1Framework

2▪ Assertion Library

4▪ Where to run tests

5▪ When to run tests

Here’s the Plan

ChaiMocha JSDOM

Node Alongside Upon save

Page 139: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Demo

This bullet list is with

animations

▪ Configure automated testing

Page 140: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Continuous Integration

Page 141: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Weird.

Works on my

machine.

- Developer without a CI server

Page 142: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Forgot to commit new file

▪ Forgot to update package.json

▪ Commit doesn’t run cross-platform

▪ Node version conflicts

▪ Bad merge

▪ Didn’t run tests

▪ Catch mistakes quickly

Why CI?

Page 143: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Run Automated build

▪ Run your tests

▪ Check code coverage

▪ Automate deployment

What Does a CI Server Do?

Page 144: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Travis

Continuous Integration

Appveyor Jenkins

Page 145: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Demo

This bullet list is with

animations

▪ Set up Continuous Integration

Page 146: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations▪ Testing frameworks

- Mocha, Jasmine, AVA, Tape, Jest…

▪ Assertion libraries

- Chai, Expect

▪ Helper libraries

- JSDOM, Cheerio

▪ Where to run tests

- Browser, Headless, In memory

▪ Where to place tests, and when to run

▪ Continuous Integration

- Travis CI, Appveyor, Jenkins

▪ Next up: HTTP and Mock APIs

Wrap Up

Page 147: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Demo

This bullet list is with

animations

▪ Production build process

- Lint and runs tests

- Bundle and minify JS and CSS

- Generate JS and CSS sourcemaps

- Exclude dev-specific concerns

- Build React in production mode

- Open prod build in browser

Page 148: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Production Builds Matter

▪ Dev

build▪ 4.8MB

▪ Prod build▪ Essentials

121K 64K!

Drop babel-polyfill, toastr, jQuery, Bootstrap

Page 149: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Flux

Page 150: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

A pattern

Centralized dispatcher

Unidrectional data flows

What is Flux?

Page 151: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

They call it Flux for a reason too.

Page 152: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Facebook’s Flux

Alt

Reflux

Flummox

Marty

Fluxxor

Delorean

Redux

NuclearJS

Fluxible

Flux Implementations

Page 153: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Good Luck Debugging This

Page 154: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Two-way binding

Two-way Binding vs Unidirectional

React

View

Action

Dispatcher

Store

Unidrectional

View

Viewmodel

Page 155: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Flux: 3 Parts

Delete User

Notify everyone who cares

Hold app state and logic

React

View

Action

Dispatcher

Store

Page 156: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Encapsulate events

Triggered by user interactions and

server

Passed to dispatcher

Actions

React

View

Action

Dispatcher

Store

Page 157: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Actions

React

View

Action

Dispatcher

Store

Payload has type and data

{

type: USER_SAVED

data: {

firstName: ‘Cory’,

lastName: ‘House;

}

}

Page 158: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Central Hub – There’s only one

Holds list of callbacks

Broadcasts payload to registered

callbacks

Sends actions to stores

Dispatcher

React

View

Action

Dispatcher

Store

Page 159: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Keeps things organized

Provides high level view of what the app actually does

Constants

Page 160: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Holds app state, logic, data retrieval

Not a model - Contains models.

One, or many

Registers callbacks with dispatcher

Uses Node’s EventEmitter

Store

React

View

Action

Dispatcher

Store

Page 161: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Every store has these common traits (aka interface)

1. Extend EventEmitter

2. addChangeListener and removeChangeListener

3. emitChange

The Structure of a Store

Page 162: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Dispatcher

Payload

Product

StoreUser Store

Address

Store

{

type: USER_SAVED

data: {

firstName: ‘Cory’,

lastName: ‘House;

}

}

Page 163: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

As an application grows, the dispatcher

becomes more vital, as it can be used to

manage dependencies between the stores

by invoking the registered callbacks in a

specific order. Stores can declaratively

wait for other stores to finish updating,

and then update themselves accordingly.Flux Documentation

Page 164: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

I usually view ActionCreators as something that is used

for writes to the server. For example, the user added a

new message by clicking the Send button, so the

responsible view calls into ActionCreator.

On the other hand, if a store needs to fetch data from

the server, then that doesn't have to use an

ActionCreator. Instead, the store can directly hit an

endpoint to load its data, and then direct the response

through the dispatcher.Jing Chen, Facebook

Page 165: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Top level component

Interacts with Stores

Holds data in state

Sends data to children as props

Controller Views

React

View

Action

Dispatcher

Store

Page 166: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React View

Action

Dispatcher

Store

Web API

Send

Action

Payload

Send

Action

Payload

Updates storage and

fires change event

Payload sent to dispatcher

Checks for registered callbacks

Sends payload to all registered

callbacks

Receives payload

Store updates and emits change event

Payload:

{

type: USER_SAVED

data: {

firstName: 'Cory',

lastName: 'House';

}

}

User clicked “Save User” button…

Receives change event and re-renders

Page 167: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React Hey CourseAction, someone clicked this “Save Course” button.

Action Thanks React! I registered an action creator with the dispatcher, so the dispatcher should take care of notifying all the stores that care.

Dispatcher Let me see who cares about a course being saved. Ah! Looks like the CourseStore has registered a callback with me, so I’ll let her know.

Store Hi dispatcher! Thanks for the update! I’ll update my data with the payload you sent. Then I’ll emit an event for the React components that care.

React Ooo! Shiny new data from the store! I’ll update the UI to reflect this!

A Chat With Flux

Page 168: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Flux API

register(function callback) – “Hey dispatcher, run me when actions happen. -

Store”

unregister(string id) – “Hey dispatcher, stop worrying about this action. - Store”

waitFor(array<string> ids) – “Update this store first. – Store”

dispatch(object payload) - “Hey dispatcher, tell the stores about this action. -

Action”

isDispatching() – “I’m busy dispatching callbacks right now.”

Page 169: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Flux is a pattern for unidirectional data flows

Actions encapsulate events

Dispatcher is a central hub that holds callbacks

Stores holds app state

Many implementations

Summary

React

View

Action

Dispatcher

Store

Page 170: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Let’s code Flux!

Page 171: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Why Redux?

Page 172: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

A Silly Number of Flux Flavors

Flux Many more…Redux

Page 173: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Two Most Popular Flavors

Flux Redux

Page 174: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 175: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React

Flux

Redux

Faceboo

k Hires

Redux

Creator

2013

2014

2015

2016

Page 176: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ One Store

▪ Reduced Boilerplate

▪ Isomorphic/Universal Friendly

▪ Immutable Store

▪ Hot Reloading

▪ Time-travel debugging

▪ Small

Why Redux?

Page 177: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.
Page 178: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Boilerplate Magic

Flux Redux

Page 179: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

@housecor reactjsconsulting.com

Cory House

Intro to Redux

Page 180: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ Do I need Redux?

▪ 3 Principles

▪ Flux vs Redux

▪ Redux Flow

Here’s The Plan

Page 181: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Do I need Redux?

Page 182: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Vanilla JS

Simple Complex

jQuery React React + Redux

No setup Significant setup

Page 183: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Complex data flows

▪ Inter-component communication

▪ Non-heirarchical data

▪ Many actions

▪ Same data used in multiple places

When Do I Need Redux?

Page 184: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

“…If you aren’t sure if you need it,

you don’t need it.”

Pete Hunt on Flux and Redux

Page 185: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Store

Action: Create User

Page 186: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

One immutable store

Redux: 3 Principles

Actions trigger changes Reducers update state

Page 187: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Flux vs Redux

Page 188: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Data down.Actions up.

Page 189: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Unidirectional Flow

Flux and Redux: Similarities

Actions Stores

Page 190: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Reducers

Redux: New Concepts

Containers Immutability

Page 191: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux ▪ Redux

Flux vs Redux

React

Action

Dispatcher

Store

React

Action

ReducersStore

Page 192: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

Stores contain state and change logic

▪ Redux

Store and change logic are separate

Page 193: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

Stores contain state and change logic

Multiple stores

▪ Redux

Store and change logic are separate

One store

Page 194: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

Stores contain state and change logic

Multiple stores

Flat and disconnected stores

▪ Redux

Store and change logic are separate

One store

Single store with hierarchical reducers

Page 195: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

Stores contain state and change logic

Multiple stores

Flat and disconnected stores

Singleton dispatcher

▪ Redux

Store and change logic are separate

One store

Single store with hierarchical reducers

No dispatcher

Page 196: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

Stores contain state and change logic

Multiple stores

Flat and disconnected stores

Singleton dispatcher

React components subscribe to stores

▪ Redux

Store and change logic are separate

One store

Single store with hierarchical reducers

No dispatcher

Container components utilize connect

Page 197: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

Stores contain state and change logic

Multiple stores

Flat and disconnected stores

Singleton dispatcher

React components subscribe to stores

State is mutated

▪ Redux

Store and change logic are separate

One store

Single store with hierarchical reducers

No dispatcher

Container components utilize connect

State is immutable

Page 198: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Redux Flow

Page 199: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Redux Flow

function appReducer(state = defaultState, action) {

switch(action.type) {

case RATE_COURSE:

//return new state

}

}

{ type: RATE_COURSE, rating: 5 }

Notified via React-ReduxReact

Action

ReducersStore

Page 200: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ If you need Redux, you’ll know it.

▪ 3 Principles

- Immutable Store

- Actions trigger changes

- Reducers return updated state

▪ Flux vs Redux

▪ Unidirectional Redux Flow

▪ Next up: Actions, stores, and reducers

Summary

Page 201: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

@housecor reactjsconsulting.com

Cory House

Actions, Store, and Reducers

Page 202: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ Actions

▪ Store

▪ Immutability

▪ Reducers

Here’s The Plan

Page 203: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

rateCourse(rating) {

return { type: RATE_COURSE, rating: rating }

}

Action Creators

Action

Action Creator

Page 204: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ let store = createStore(reducer);

Creating Redux Store

Page 205: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ store.dispatch(action)

▪ store.subscribe(listener)

▪ store.getState()

▪ replaceReducer(nextReducer)

Redux Store

Page 206: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Immutability

Page 207: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Immutability:To change state, return a new object.

Page 208: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Immutable already! ☺

Number

String,

Boolean,

Undefined,

Null

▪ Mutable

Objects

Arrays

Functions

What’s Mutable in JS?

Page 209: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

state = {

name: 'Cory House'

role: 'author'

}

state.role = 'admin';

return state;

Current state

Traditional App - Mutating state

Page 210: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

state = {

name: 'Cory House'

role: 'author'

}

return state = {

name: 'Cory House'

role: 'admin'

}

Current state

Returning new object. Not mutating state! ☺

Page 211: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Signature

Object.assign(target, ...sources)

Example

Object.assign({}, state, {role: 'admin'});

Copy

Page 212: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

State is mutated

▪ Redux

State is immutable

Page 213: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Clarity

▪ Performance

▪ Awesome Sauce

Why Immutability?

Page 214: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

“Huh, who changed that state?”

Immutability = Clarity

The reducer, stupid!

Page 215: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Clarity

▪ Performance

▪ Awesome sauce

Why Immutability?

Page 216: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

state = {

name: 'Cory House'

role: 'author'

city: 'Kansas City'

state: 'Kansas'

country: 'USA'

isFunny: 'Rarely'

smellsFunny: ’Often'

...

}

Immutability = Performance

Has this changed?

Page 217: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

if (prevStoreState !== storeState) ...

Page 218: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Clarity

▪ Performance

▪ Awesome Sauce

Why Immutability?

(Amazing debugging)

Page 219: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Time-travel debugging

▪ Undo/Redo

▪ Turn off individual actions

▪ Play interactions back

Immutability = AWESOME SAUCE!

Page 220: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Handling Immutability

Page 221: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

ES6

▪ Object.assign

▪ Spread operator

ES5

▪ Lodash merge

▪ Lodash extend

▪ Object-assign

Libraries

• react-addons-update

• Immutable.js

Handling Immutable State

JavaScript's primitives are immutable.

Page 222: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Trust

How Do I Enforce Immutability?

redux-immutable-state-

invariant

Immutable.js

Page 223: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Reducers

Page 224: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

What is a Reducer?

▪ function myReducer(state, action) {

▪ // Return new state based on action passed

▪ }

Page 225: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

(state, action) => state

Page 226: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

What is a Reducer?

▪ function myReducer(state, action) {

▪ // Return new state based on action passed

▪ }

So approachable.

So simple.

So tasty.

Page 227: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

What is a Reducer?

▪ function myReducer(state, action) {

switch (action.type) {

case ‘INCREMENT_COUNTER’:

state.counter++;

return state;

}

▪ }

Uh oh, can’t do this!

Page 228: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

What is a Reducer?

▪ function myReducer(state, action) {

switch (action.type) {

case ‘INCREMENT_COUNTER’:

return (Object.assign(

{},

state,

{counter: state.counter + 1}

);

}

▪ }

Page 229: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Reducers must be pure.

Page 230: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Mutate arguments

▪ Perform side effects

▪ Call non-pure functions

Forbidden in Reducers

Page 231: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

1 Store. Multiple Reducers.

Page 232: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

New State

authors

loadStatu

s

courses

All Reducers Are Called on Each Dispatch{ type: DELETE_COURSE, 1 }

Page 233: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Reducer = “Slice” of State

authors

courses

loadingStatus

Store

Page 234: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

“Write independent small reducer functions

that are each responsible for updates to a

specific slice of state. We call this pattern

“reducer composition”. A given action could

be handled by all, some, or none of them.”Redux FAQ

Page 235: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ Actions

- Represent user intent

- Must have a type

▪ Store

- dispatch, subscribe, getState…

▪ Immutability

- Just return a new copy

▪ Reducers

- Must be pure

- Multiple per app

- Slice of state

Summary

Next up: Connecting React to Redux

Page 236: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

@housecor reactjsconsulting.com

Cory House

Connecting React to Redux

Page 237: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ Container vs Presentation Components

▪ React-Redux

- Provider

- Connect

▪ A Chat with Redux

Here’s The Plan

Page 238: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Container

Focus on how things work

Aware of Redux

Subscribe to Redux State

Dispatch Redux actions

Generated by react-redux

▪ Presentational

Focus on how things look

Unaware of Redux

Read data from props

Invoke callbacks on props

Written by hand

Two Component Types

Page 239: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Connecting React to Redux

Page 240: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React

Action

ReducersStore

react-redux

Page 241: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Connect

Creates container components▪ Attaches app to store

Provider

React-Redux

Page 242: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

<Provider store={this.props.store}>

<App/>

</Provider>

React-Redux Provider

Page 243: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Wraps our component so it’s connected to the Redux store.

Connect

export default connect(

mapStateToProps,

mapDispatchToProps

)(AuthorPage);

Page 244: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

onChange() {this.setState({ authors: AuthorStore.getAll() });

}

Flux

componentWillMount() {

AuthorStore.addChangeListener(this.onChange);

}

componentWillUnmount() {

AuthorStore.removeChangeListener(this.onChange);

}

Page 245: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

function mapStateToProps(state, ownProps) {

return {appState: state.authorReducer };

}

export default connect(

mapStateToProps,

mapDispatchToProps

)(AuthorPage);

Redux

Benefits:

1. No manual unsubscribe

2. No lifecycle methods required

3. Declare what subset of state you want

4. Enhanced performance for free

Page 246: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

connect(mapStateToProps, mapDispatchToProps)

function mapStateToProps(state) {

return {

appState: state

};

}

React-Redux Connect

What state should I expose as props?

Page 247: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Memoize for performance

Reselect

Page 248: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

connect(mapStateToProps, mapDispatchToProps)

function mapDispatchToProps(dispatch) {

return {

actions: bindActionCreators(actions, dispatch)

};

}

React-Redux Connect

What actions do I want on props?

Page 249: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

this.props.dispatch(loadCourses());

function mapDispatchToProps(dispatch) {

return {

loadCourses: () => {

dispatch(loadCourses());

}

};

}

function mapDispatchToProps(dispatch) {

return {

actions:

bindActionCreators(actions, dispatch)

};

}

Ignore it. Use dispatch.

3 Ways to Handle mapDispatchToProps

Manually wrap

Use bindActionCreators

Page 250: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

// In component...

this.props.dispatch(loadCourses())

Two downsides

1. Boilerplate

2. Redux concerns in child components

Option 1: Use Dispatch Directly

Page 251: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

function mapDispatchToProps(dispatch) {

return {

loadCourses: () => {

dispatch(loadCourses());

},

createCourse: (course) => {

dispatch(createCourse(course));

},

updateCourse: (course) => {

dispatch(updateCourse(course));

}

};

}

// In component...

this.props.loadCourses()

Option 2: Wrap Manually

Page 252: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

function mapDispatchToProps(dispatch) {

return {

actions: bindActionCreators(actions, dispatch)

};

}

// In component:

this.props.actions.loadCourses();

Option 3: bindActionCreators

Wraps action creators in dispatch call for you!

Page 253: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React Hey CourseAction, someone clicked this “Save Course” button.

Action Thanks React! I will dispatch an action so reducers that care can update

state.

Reducer Ah, thanks action. I see you passed me the current state and the action

to perform. I’ll make a new copy of the state and return it.

Store Thanks for updating the state reducer. I’ll make sure that all connected

components are aware.

React-Redux Woah, thanks for the new data Mr. Store. I’ll now intelligently determine

if I should tell React about this change so that it only has to bother with

updating the UI when necessary.

React Ooo! Shiny new data has been passed down via props from the store! I’ll

update the UI to reflect this!

A Chat With Redux

Page 254: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations▪ Container vs Presentation Components

▪ React-Redux

- Provider

- Connect

• mapStateToProps

• mapDispatchToProps

▪ Next up: Let’s code Redux!

Time to code!

Page 255: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

@housecor reactjsconsulting.com

Cory House

Async in Redux

Page 256: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ Why a mock API?

▪ Async libraries

▪ Implement Thunks

Here’s the plan

Page 257: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This slide is with

animations

▪ Start before the API exists

▪ Independence

▪ Backup plan

▪ Ultra-fast

▪ Test slowness

▪ Aids testing

▪ Point to the real API later

Why a Mock API?

Page 258: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Flux

Handed in action

▪ Redux

?

Async

Page 259: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

redux-sagaredux-promiseredux-thunk

Redux Async Libraries

Page 260: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

▪ Thunks

Functions

Clunky to test

Easy to learn

▪ Sagas

Generators

Easy to test

Hard to learn

Comparison

Page 261: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

export function deleteAuthor(authorId) {

return dispatch => {

return AuthorApi.deleteAuthor(authorId).then(() => {

dispatch(deletedAuthor(authorId));

}).catch(handleError);

};

}

Thunk

Page 262: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Demo

This bullet list is with

animations

▪ Let’s get thunky.

Page 263: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

This bullet list is with

animations

▪ Thunks, Redux-Promise, and Sagas

▪ Naming async actions

- Consider SUCCESS and ERROR

▪ First thunk, complete!

▪ Next up: Async writes

Wrap Up

Page 264: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Final Thoughts…

Page 265: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

The Competition is Responding.

Page 266: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Angular 1 Angular 2 React

Isomorphic x x

Unidirectional x x

1 file per

component

x x

Virtual DOM x x

Hot Reloading x

Browser Support IE8 IE9 IE9

Pluggable x

JSX x

Component Syntax 4 6 10

Page 267: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Yep. But it still won’t be React.

Competition Will Eventually Have All This.

Lightweight, fast, pluggable, isomorphic components in IE8+.

Page 268: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Why React?

Fast

Pluggable

Simple

Testable

Rich Error Messaging

Composable

Broad Browser Support

Isomorphic Friendly

Battle Proven

Page 269: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

React Downside

Page 270: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

I recently announced my solution…

Page 271: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

github.com/coryhouse/react-slingshot

Page 272: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

@housecor

There’s much more to be said...

Page 273: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

bitnative.com/handouts

Page 274: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

Invitation: 5 Minute Retrospective

Page 275: React From Scratch - Software Design & Development 2020sddconf.com/brands/sdd/library/React_from_Scratch_-_SDD_2017.pdf · Development environment Testing Redux Here’s the Plan.

speakerrate.com@housecor pluralsight.com