Ember & React

Made Easy

Gordon Hempton - 11/18 - Seattle React

Who's this dude?

What is Ember.js?

What is React?

Two Frameworks? Are you Crazy?

Ember + Ember-Data + jQuery + Handlebars ~= 811k (minified)

React ~= 126k (minified)

Why Switch To React?

Worst framework performance comparison ever:

Initial Render is Our Bottleneck

Ember is heavily optimized for changes, not the initial render

Our App

outreach.io

Perf Optimization Sucks

Some solutions:

What we want:

React does a good job at this

Re-render everything, anytime you want

Incremental optimizations

shouldComponentUpdate

Enter Ember-React

Simple set of utilities to make using React inside of Ember easy

Works inside of existing Ember applications

Demo

React components

// file: app/react/time-ago.js
/** @jsx React.DOM */
export default React.createClass({
  render: function() {
    var date = this.props.date;
    return (
      <time datetime={date}>{moment(date).fromNow()}</time>;
    );
  }
});

Inside your Ember templates

<div>{{react 'time-ago' date=date}}<div>

React Router

Based on the Ember.js Router :)

React Routing

<Routes location="history">
  <Route name="profiles"
    path={rootPath + '/profiles'}
    handler={ProfilesIndex} />
  <Route name="routing"
    path={rootPath + '/routing'}
    handler={Routing} />
</Routes>

Inside your Ember Routes

export default Ember.Route.extend({
  renderTemplate: function() {
    var rootPath = this.router.generate(this.routeName);
    this.render({
      react: ...
    });
  }
});

Inside the Ember Router DSL

Router.map(function() {
  this.react('posts');
});

Observations

When to Use React within an existing Ember app?

Ember 2.0

Both Angular 2.0 and Ember 2.0 look a lot like React

From the Ember 2.0 RFC:

In Ember 2.0, we will be adopting a "virtual DOM" and data flow model that embraces the best ideas from React and simplifies communication between components.

We plan to transition to: when a route is entered, it renders a component, passing along the model as an attr. This eliminates a vestigial use of old-style views, and associates the top-level template with a regular component.

Ember 2.0

Our React Stack

Ember 2.0 Our React Stack
  • Components
  • Templates
  • Routes
  • Models (Ember Data)
  • Container (DI)
  • Conventions
  • Components
  • JSX
  • React-Router
  • Models (Coalesce)
  • React Context (DI)
  • Conventions (Ember's)

More and more of our app is becoming React

Things Ember does well

What about Flux?

See Also

Thanks!

@ghempton