Slide 1

Slide 1 text

Migrating safely to React (part 2) @jamischarles React Amsterdam 2016

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

UI Engineer Send Money San Jose, CA

Slide 4

Slide 4 text

Back

Slide 5

Slide 5 text

2014

Slide 6

Slide 6 text

C++ XML Markup

Slide 7

Slide 7 text

Server {Dust.js} Client Kraken.js BB

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

• too many abstraction layers • low confidence • difficult state bugs (currency codes mismatched etc) Pain

Slide 10

Slide 10 text

• V1: Just ship it • Maintainability is low priority • Complexity rises What happened?

Slide 11

Slide 11 text

by Rich Hickey Clojure Simple Made Easy

Slide 12

Slide 12 text

Speed Time Easy

Slide 13

Slide 13 text

Speed Time Simple

Slide 14

Slide 14 text

Speed Time Easy Simple

Slide 15

Slide 15 text

“Simple → predictable → Reliable”

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

1 feature →

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

• React simplifies the view (fewer files) • Quick win • Push it to prod • Learn & evaluate Lessons learned

Slide 23

Slide 23 text

Beyond the view

Slide 24

Slide 24 text

1. View layer 2. Routing layer 3. Data layer The pieces

Slide 25

Slide 25 text

• Backbone Router + custom routing logic • complex • difficult to track url -> code • good opportunity Routing Layer

Slide 26

Slide 26 text

/request Routing /send

Slide 27

Slide 27 text

/request Routing /send

Slide 28

Slide 28 text

/request Routing /send

Slide 29

Slide 29 text

/request Routing /send

Slide 30

Slide 30 text

/request Routing /send

Slide 31

Slide 31 text

require('backboneSubroute'); module.exports = Backbone.SubRoute.extend({ routes: { '': 'showTransfer', ':type(/:action)': 'showTransfer' }, […]

Slide 32

Slide 32 text

require('backboneSubroute'); module.exports = Backbone.SubRoute.extend({ showView: function (options) { if (options.name) { this.loadScripts(options); } historyModel.addPath(options.name); }, getPath: function (scriptName) { // getPath code }, loadScripts: function (options) { // loadScripts code }, cleanView: function () { // cleanView code } });

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

// React Components... import MainContainer from '../containers/main'; import RequestContainer from '../containers/request'; import SendContainer from '../containers/send'; import BuyContainer from '../containers/buy'; ReactDOM.render( {/* Flow entry points */} , document.getElementById('react-transfer-container'));

Slide 35

Slide 35 text

/request Routing /send

Slide 36

Slide 36 text

import React from 'react'; import indexView from '../index'; let SendContainer = React.createClass({ propTypes: { params: React.PropTypes.object.isRequired }, componentDidMount() { this.showBBView(); }, componentDidUpdate() { this.showBBView(); }, // will load and show a Backbone view. showBBView() { let type = 'send'; let action = this.props.params.action || 'recipient'; // gets content and renders it into the DOM. indexView.navigate(type, action); }, render() { return null; } }); export default SendContainer;

Slide 37

Slide 37 text

import React from 'react'; import indexView from '../index'; let SendContainer = React.createClass({ propTypes: { params: React.PropTypes.object.isRequired }, componentDidMount() { this.showBBView(); }, componentDidUpdate() { this.showBBView(); }, // will load and show a Backbone view. showBBView() { let type = 'send'; let action = this.props.params.action || 'recipient'; // gets content and renders it into the DOM. indexView.navigate(type, action); }, render() { return null; } }); export default SendContainer;

Slide 38

Slide 38 text

import React from 'react'; import indexView from '../index'; let SendContainer = React.createClass({ propTypes: { params: React.PropTypes.object.isRequired }, componentDidMount() { this.showBBView(); }, componentDidUpdate() { this.showBBView(); }, // will load and show a Backbone view. showBBView() { let type = 'send'; let action = this.props.params.action || 'recipient'; // gets content and renders it into the DOM. indexView.navigate(type, action); }, render() { return null; } }); export default SendContainer;

Slide 39

Slide 39 text

import React from 'react'; import indexView from '../index'; let SendContainer = React.createClass({ propTypes: { params: React.PropTypes.object.isRequired }, componentDidMount() { this.showBBView(); }, componentDidUpdate() { this.showBBView(); }, // will load and show a Backbone view. showBBView() { let type = 'send'; let action = this.props.params.action || 'recipient'; // gets content and renders it into the DOM. indexView.navigate(type, action); }, render() { return null; } }); export default SendContainer;

Slide 40

Slide 40 text

/request Routing /send

Slide 41

Slide 41 text

/request Routing /send

Slide 42

Slide 42 text

/request Routing /send

Slide 43

Slide 43 text

import React from 'react'; import * as routeUtil from '../utils/routeHelper'; let SendContainer = React.createClass({ propTypes: { params: React.PropTypes.object.isRequired }, componentDidMount () { this.showBBView(); }, componentDidUpdate() { this.showBBView(); }, // will load and show a Backbone view. showBBView() { let type = 'send'; let action = this.props.params.action || 'recipient'; // gets content and renders it into the DOM. routeUtil.getIndexView().navigate(type, action); }, render() { return null; } }); export default SendContainer;

Slide 44

Slide 44 text

import React from 'react'; import * as routeUtil from '../utils/routeHelper'; let SendContainer = React.createClass({ propTypes: { params: React.PropTypes.object.isRequired }, render() { return } }); export default SendContainer;

Slide 45

Slide 45 text

• Url → code is easy now • Reduced complexity • Good migration path for a give url Routing Layer

Slide 46

Slide 46 text

• Backbone Models & Collections • complex • Most of our state pain comes from here Data Layer

Slide 47

Slide 47 text

Migrate 1 feature?

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

new feature

Slide 50

Slide 50 text

• Larger time investment • Greater risk • More testing / QA needed What about old data?

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Optimize for future change

Slide 55

Slide 55 text

– Michael Feathers “You have to build a barrier between yourself and the framework.”

Slide 56

Slide 56 text

– Jamis Charles “Use as much vanilla JS as possible.”

Slide 57

Slide 57 text

import $ from ‘jquery'; import Backbone from ‘backbone'; export default Backbone.View.extend({ el: 'form', events: { 'keyup #note': 'resizeTextAreaField', 'blur #note': 'hideTextAreaCounters' }, // Resize text area as user types resizeTextAreaField: function (event) { var count = this.countTextContent(); var newHeight = this.getIdealTextAreaHeight(count); var newRows = this.getRowsFromHeight(newHeight); $('#note').height(newHeight).attr('rows', newRows); }, // Update textarea counter with remaining character left hideTextAreaCounters: function (event) { // pseudo code },

Slide 58

Slide 58 text

import $ from ‘jquery'; import Backbone from ‘backbone'; export default Backbone.View.extend({ el: 'form', events: { 'keyup #note': 'resizeTextAreaField', 'blur #note': 'hideTextAreaCounters' }, // Resize text area as user types resizeTextAreaField: function (event) { var count = this.countTextContent(); var newHeight = this.getIdealTextAreaHeight(count); var newRows = this.getRowsFromHeight(newHeight); $('#note').height(newHeight).attr('rows', newRows); }, // Update textarea counter with remaining character left hideTextAreaCounters: function (event) { // pseudo code },

Slide 59

Slide 59 text

import $ from ‘jquery'; import Backbone from ‘backbone'; import * as ResizeUtil from 'utils/resizeTextareaUtil'; export default Backbone.View.extend({ el: 'form', events: { 'keyup #note': 'resizeTextAreaField', 'blur #note': 'hideTextAreaCounters' }, // Resize text area as user types resizeTextAreaField: ResizeUtil.resizeField, // Update textarea counter with remaining character left hideTextAreaCounters: ResizeUtil.hideTextArea });

Slide 60

Slide 60 text

In Conclusion

Slide 61

Slide 61 text

Solve real problems

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Thank you

Slide 64

Slide 64 text

Sources • React Rally talk: https://www.youtube.com/ watch?v=mrtwImsEq5s • Moving from Require to Webpack 
 https://www.paypal-engineering.com/ 2015/08/07/1450/ • http://www.infoq.com/presentations/Simple- Made-Easy

Slide 65

Slide 65 text

@jamischarles #reactamsterdam

Slide 66

Slide 66 text

No content