Skip to content
Swizec Teller - a geek with a hatswizec.com

Backbone → React: it's a people problem after all ?

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.

Did you enjoy this article?

Published on September 21st, 2016 in backbone, backbonejs, Front End, react, reactjs, Technical

Learned something new?
Want to become a high value JavaScript expert?

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 thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their JS careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️