Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Backbone to React: It's a People Problem After All

DZone's Guide to

Backbone to React: It's a People Problem After All

The author discusses the first necessary tradeoff when converting his Backbone app to React.

· Web Dev Zone
Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

The Big Scary Refactor™, turning a largish, hard-to-manage Backbone app into an easier to manage React app… it got off to a great start yesterday. Today, not so much.

First of all, I realized that our problem isn’t Backbone. The problem is that our UI is not a function of state. It’s an expression of state and a sequence of user actions.

I mean, observing changes and reacting to them is spectacular in theory. But I've never seen it work long-term in practice. 

 Follow

Starting to realize our problem isn't Backbone. Our problem is that I didn't enforce the UI-as-expression-of-data rule well enough.

To give you a simple example, we use this pattern a lot:

class Thing extends Backbone.View {
    constructor() {
        this.events {
            "click button": "toggleActive"
        }
    }

    toggleActive() {
        if (checkSomething) {
            this.$('button').addClass('active');
        }else{
            this.$('button').removeClass('active');
        }
    }
}

This is a trivial Backbone view. It listens for a click event on a<button>. When a click happens, it either adds or removes an activeclass on that button.

Our real views do the same sort of thing, but on a grander scale. It might seem harmless enough, but if your code has this pattern, you can’t reliably wrap Backbone views in React components. You’d have to replay all user interaction on every re-render.

That’s a no-go.

A better approach, which we follow most of the time (but of course not all of the time), is to move that checkSomething into the View’s backing Model. Something like this:

class Thing extends Backbone.View {
    constructor() {
        this.events {
            "click button": "toggleActive"
        }
        this.model = new Backbone.Model({active: false});
        this.listenTo(this.model, 'change', this.render);

        this.template = Handlebars.compile('<button class="{{#if active}}active{{/if}}">Button</button>');
    }

    toggleActive() {
        this.model.set('active', !this.model.get('active'));
    }

    render() {
        this.$el.html(this.template(this.model.attributes));
    }
}

Now the UI is an expression of state and user actions manipulate that state instead of the UI. You can re-render as much as you want, and your UI will look correct.

But why re-render in the first place? Because it’s the easiest way to deal with data changes from above.

We’re moving towards a modern unidirectional data flow architecture where application state lives in a central Redux or MobX store. Right now, it’s scattered through a bunch of Backbone Models living inside each View individually. Then they interact.

It’s a mess. Did I mention we have untraceable heisenbugs? Many.

If you want unidirectional, then The Central Store is your single source of truth. That means that when new state comes down from the top, you follow.

With a React component, your props change, React calls render(), and your UI updates.

When your React component is just a thin wrapper for a Backbone View, well… then you have a choice. Update the view’s internal model and hope for the best, or throw the view away and render a new one… and once again hope for the best.

There’s a lot of hoping for the best, but I think the “throw away and re-render” is going to be easier to deal with in the long run. It’s also better when we go through the process and all those child Backbone Views eventually become proper React components.

Replaying user interaction to exactly match UI on re-render? Yeah, fuck that. That’s not gonna work with either strategy. 

Ok, so we have our first tradeoff: To avoid UI staleness, we’re going to have UI that forgets user interactions.

Such is life ¯\_(ツ)_/¯

Tomorrow, I’ll show you why I think MobX can’t work in our situation, but Redux might.

PS: If you wanna help, we’re hiring. Ping me.

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
backbone.js ,react.js

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}