Slide 1

Slide 1 text

Migrating safely to React (part 2) @jamischarles

Slide 2

Slide 2 text

UI Engineer Send Money

Slide 3

Slide 3 text

Back

Slide 4

Slide 4 text

2014

Slide 5

Slide 5 text

C++ XML Markup

Slide 6

Slide 6 text

Server {Dust.js} Client Kraken.js BB

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

by Rich Hickey Clojure Simple Made Easy

Slide 11

Slide 11 text

Speed Time Easy

Slide 12

Slide 12 text

Speed Time Simple

Slide 13

Slide 13 text

Speed Time Easy Simple

Slide 14

Slide 14 text

“Simple → predictable → Reliable”

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

1 feature →

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Beyond the view

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

/request Routing /send

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

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

Slide 31

Slide 31 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 32

Slide 32 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 33

Slide 33 text

/request Routing /send

Slide 34

Slide 34 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 35

Slide 35 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 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

/request Routing /send

Slide 39

Slide 39 text

/request Routing /send

Slide 40

Slide 40 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 41

Slide 41 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 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Migrate 1 feature?

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

new feature

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Optimize for future change

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 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 56

Slide 56 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 57

Slide 57 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 58

Slide 58 text

In Conclusion

Slide 59

Slide 59 text

Solve real problems

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Thank you

Slide 62

Slide 62 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 63

Slide 63 text

@jamischarles #reactamsterdam

Slide 64

Slide 64 text

No content