Over a million developers have joined DZone.

Backbone > React: Handling State With Mobx

DZone's Guide to

Backbone > React: Handling State With Mobx

Connecting old backbone views with data flow components in React.

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

I did it! I figured out a way to reliably interop between modern unidirectional data flow React components and old Backbone views. It ain’t pretty, but it works.

There are three components in that page:

  • A CurrentCount component that shows current counter state; it’s pure React
  • A ButtonWrapper component that shows counter state and does +1 on click; it’s a wrapped Backbone view
  • A ReactButton component that does +10 on click; it’s also pure React

I know this is a trivial example, but it shows a powerful concept. That +1 button is still the same Backbone view from Tuesday. It stores state in a local Backbone Model, it uses a Handlebars template, and it remains the same idiomatic Backbone View it’s always been. Yet it interops with the React app, blissfully unaware that something freaky's going on.

The Backbone View

All three components share the same MobX data store, which has a single observable value called N. It looks like this:

class CounterStore {
@observable N = 0

In MobX, stores are classes with observable properties. They often have methods and computed values as well, but this example is too simple.

@observable is a decorator that compiles into something like makeObservable(this.N, 0), which in turn uses ES6 to add magical getters and setters that fire up the MobX engine whenever you access – dereference – the observable value. Doing it yourself would look like this:

class CounterStore {
N = 0

set N (val) {
this.N = val;

// notify all observers that N has changed

get N () {
// add call site to list of observers

return N;

MobX saves you from writing that logic yourself, and it adds a bunch of smartness to make it fast and efficient. I don’t really know how the engine works, but after reading the docs and some of Michel Weststrate’s Medium posts, I’m convinced it’s amaze.

So that’s the store – no boilerplate involved. The two pure React components don’t involve much boilerplate either.

class ReactButton extends Component {
    buttonClicked() {
        this.props.counterStore.N += 10;

    render() {
        return (
                <p>React Button:</p>
                <button onClick={action('inc-counter', this.buttonClicked.bind(this))}>Jump click count +10</button>

@inject('counterStore') @observer
class CurrentCount extends Component {
    render() {
        const { N } = this.props.counterStore;
        return (<p>Current count in counterStore: {N}</p>)

The @inject decorator takes props from a React context and adds them to a component. I don’t know how MobX-specific this is, but it reduces our boilerplate. Instead of giving each component a store={this.props.store} type of prop, we can wrap the whole thing in a <Provider> and give everyone access.

At the end of the day, you always realize that all your components need access to your application state. Global singletons for things everyone needs make life easier. Trust me.

The @observer decorator comes from MobX’s React bindings. It automagically makes the render() method listen to store changes, but only those changes that it uses.

This is key. It’s what makes MobX fast. It’s also what leads to confusion when you’re doing things that are not idiomatic React, like inserting Backbone views into React components.

Thanks to Michel Weststrate for helping me out. His tip about using autorun saved the day

And with that out of the way, I had a Backbone view wrapped in a React component, including full data interop. Change local Backbone state, and the global data store finds out, changes the data store, and the Backbone view updates.

It looks like this:

@inject('counterStore') @observer
class ButtonWrapper extends Component {
    constructor(props) {



    _init() {
        this.button = new BackboneButton({
            N: this.props.counterStore.N
        this.button.model.on('change:N', action('inc-counter', (_, N) => {
            this.props.counterStore.N = N;

    componentDidUpdate() { this._render(); }
    componentDidMount() { this._render(); }

    _render() {

    componentWillUnmount() { this._cleanup(); }

    _cleanup() {

    render() {
        return (
                <p>Backbone Button:</p>
                <div className="button-anchor" ref="anchor" />

Like I said, it ain’t pretty

It works like this:

  • React renders an anchor div
  • On component mount or update, it runs Backbone view rendering in _render
  • On component unmount, it cleans up Backbone’s DOM event listeners
  • Inside _render it: 
    • Cleans up Backbone DOM event listeners
    • Instantiates a new Backbone view with _init
    • Tells the view to render inside that anchor <div>
  • when initializing the view in _init it: 
    • Creates a new BackboneButton instance and gives it the value of N from our store
    • Listens for changes to N on the view’s internal state and communicates them upstream with a MobX action

This approach is a leaky abstraction. The _init method has to know intimate details about the Backbone view you’re wrapping. There’s no way to get around that because MobX needs those getters and setters to observe state changes.

As soon as you pass a value, MobX loses track. I tried passing the whole store into a Backbone view and using it directly as a model, but that didn’t work. Backbone Model’s set() and get() methods circumvent native getters and setters, which means MobX can’t track changes or uses.

Another issue is that because our Backbone views aren’t pure, the UI could sometimes look wrong to the user. It won’t be stale, but it won’t show all side-effects from user actions either.

But we can deal with that later. The important part is that we have a way forward! A way to go from Backbone to React without resorting to a full complete rewrite of everything from scratch. \o/

You should follow me on twitter, here.

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.

backbone ,components ,model ,react

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}