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

Benchpress your Legacy Applications With React

Benchpress your Legacy Applications With React

Also known as Migrating from Backbone to React.

Presented at ReactNYC https://www.meetup.com/ReactNYC/events/242406154/

Harry Wolff

October 30, 2017
Tweet

More Decks by Harry Wolff

Other Decks in Programming

Transcript

  1. HISTORY A lot of our data currently exists in Backbone

    Models. const SettingsModel = Backbone.Model.extend({ isFreeTierPlan() { return this.get('planType') === 'FREE_TIER'; }, }); const settings = new SettingsModel({ planType: 'FREE_TIER' }); console.log(settings.isFreeTierPlan()) // true
  2. HISTORY Backbone.Models emits events when its values change. settings.on('change', function(model)

    { assert(model === settings); // true assert(model.get('hello') === 'world'); // true }); settings.set('hello', 'world');
  3. HISTORY Marionette.Views are built to be used with Backbone.Models. They

    listen to change events in a Backbone.Model and update their template.
  4. ▸ https://github.com/mongodb-js/connect-backbone-to- react/ ▸ It removes a lot of boilerplate

    code. ▸ It standardizes an API between Backbone and React. ▸ Mirrors the API of react-redux for a familiar and friendly experience. ▸ It can be tested in isolation. connectBackboneToReact is a library that makes it easy to use Backbone Models within React. CONNECTBACKBONETOREACT
  5. CONNECTBACKBONETOREACT connectBackboneToReact has rich options. ▸ You can choose which

    Backbone.Model attributes to provide to your React component. ▸ You can define which events you want to listen to. ▸ You can debounce the render event to prevent unnecessary re-renders. ▸ ….and more!
  6. CONNECTBACKBONETOREACT It makes it possible for us to gradually migrate

    from Backbone to React without a complete rewrite.
  7. CONNECTBACKBONETOREACT Without connectBackboneToReact we would have to write this every

    time we used a Backbone.Model: class MyComponent extends React.Component { componentWillMount() { this.props.model.on('change', this.updateState, this); } componentWillReceiveProps(nextProps) { this.updateState(nextProps.model); } componentWillUnmount() { this.props.model.off('change', this.updateState, this); } updateState(model) { this.setState({ data: model.toJSON() }); } render() { return ( <div>{this.state.data.property}</div> ) } } // Usage <MyComponent model={settings} />
  8. CONNECTBACKBONETOREACT With connectBackboneToReact class MyComponent extends React.Component { render() {

    return ( <div>{this.props.settings.property}</div> ) } } const MyComponentConnected = connectBackboneToReact()(MyComponent); // Usage <MyComponentConnected model={{ settings: settings }} />