Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Backbone to React: How to write great code

From Backbone to React: How to write great code

Richard spent the last 16 months migrating a large Backbone codebase to React. Learn why this has made their team so very happy and what it can say about good code vs great code.

Frontend NE

January 07, 2016
Tweet

More Decks by Frontend NE

Other Decks in Technology

Transcript

  1. data = _.extend({ isHighlighted: this.isHighlighted }, this.user.toJSON(), this.account.toJSON() }); html

    = Mustache.render(Template, data); this.$el.html(template); return this.$el;
  2. var classes = "user-wrapper" if (this.state.isHighlighted) { var classes +=

    " is-highlighted" } return ( <div className={classes}> <strong>{this.props.userName}</strong> <img src={this.props.avatar} /> <span>account: {this.props.accountName} </div> );
  3. if (!Array.isArray(instances)) { type = typeof instances; msg = "listenToEvents

    expects array,not:"; console.warn(msg + " " + type); }
  4. “our powers to visualize processes evolving in time are relatively

    poorly developed Edsger W. Dijkstra http://bit.ly/1fzrqcm
  5. Data 1. WillRecieveProps 2. ShouldUpdate 3. WillUpdate 4. Render 5.

    DidUpdate component 1: component 2: Data 1. WillRecieveProps 2. ShouldUpdate 3. WillUpdate 4. Render 5. DidUpdate
  6. Data lifecycle methods component 1: lifecycle methods lifecycle methods lifecycle

    methods lifecycle methods component 2: component 3: component 4: component 5:
  7. initialize: function(model) { this.model = model this.listenTo( this.model, "change:name", this.changeName

    ) } changeName: function() { name = this.model.get('name') this.$el.find('.name').html(name) }
  8. initialize: function(model) { this.model = model this.listenTo( this.model, "change:name", this.changeName

    ) } changeName: function() { name = this.model.get('name') this.$el.find('.name').html(name) }
  9. 1. lifecycle methods 1. render? 2. manual dom updates? 3.

    data binding? 4. updating children?
  10. 1. Defend against future mistakes 2. Make the model of

    time simpler 3. Standardise the effects of time predictability:
  11. “"A highly composable system provides ... components that can be

    selected and assembled in various combinations to satisfy specific user requirements" blog.kissmetrics.com/loading-time/
  12. Backbone: Extend a class? SelectableRow = Row.extend({ events: function() {

    oldEvents = Row.prototype.events; newEvents = {'click': 'select'}; return _.extend(oldEvents, newEvents}); } template: require('selectable-row.html'), getViewData: function() { return model.toJSON(); } });
  13. toggleOpen: function() { this.setState({open: !this.state.open}) }, render: function() { return

    ( <DropdownControls toggleOpenState={this.toggleOpen} open={this.state.open} > <div>shown when state.open</div> </DropdownControls> ) } React: Add a child component
  14. 1. Fewer regressions 2. Less serious regressions 3. More code

    re-use 4. Shipping faster
 5. Better usability & UX