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.

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 active class 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.

Learned something new? Want to become a better engineer? 💌

Join 9,400+ people just like you already improving their skills.

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖 right away. After that you'll get a thoughtfully written email every week about React, JavaScript,  and lessons learned in my 20 years of writing code for companies ranging from tiny startups to Fortune5 behemoths.



Man, I love your way of writing these newsletters. Often very relatable and funny perspectives about the mundane struggles of a dev. Lightens up my day. ~ Kostas

PS: You should also follow me on twitter 👉 here.
It's where I go to shoot the shit about programming.