As I dug into this part of our code more, it became apparent that we have some of the Track Nav state on the DOM in data attributes as well as in the Backbone models/collections.
Given the information above, I definitely wanted to do a spike on rewriting the Track Nav in React to see if we could address some of the pain points involved with maintaining and updating the Track Nav.
As I approached this, there were a few things I wanted to do and not do:
With the three objectives above, I decided to only rewrite the Backbone views as React components. If we want to rewrite the rest of the site, that is probably the right time to bring in the Flux architecture and completely replace the Backbone infrastructure.
In order to swap out the Backbone template rendering with React components, I just followed these steps:
render
function. For us this was in the TrackNavView
view.onClick
, etc.), you can pass them in as props to the React component. This way, the only way you’re interacting with the external data is through the existing functions on your Backbone view.With the above steps, let’s take a quick look at how we did against our goals.
As I mentioned before, we configured Backbone to render the entire tree view upfront. You can see that occuring on the large green section in the screenshot below.
Here’s the timeline after we rewrote it in React to defer the rendering until the user clicks to open the nav. The entire process of rendering the nav is no where to be seen! (Note that the timeline magnification is large in the screenshot below)
Since we passed in the Backbone model and collections as props, any changes to them from other apps cause the React components to rerender if necessary.
One of the features of Backbone/Marionette is that it can automatically render collection of views based on a Backbone collection. This is usually a great feature, but when its being used for a hierarchy where some of the nodes like the leaf nodes are displayed and interacted with differently, it can be hard to follow the code.
In the React rewrite, I ended up with components specific to each node level with the different states of the node (i.e. active, open, completed, in progress) written declaratively. It was much easier than manipulating the data attributes of the DOM elements and having to match up the CSS styles based on data attributes.
When rewriting a part of your app, it’s always important to consider the time investment you’re making and the benefits that will come with the rewrite. In this particular case, I think the day to rewrite the code was well worth it and hopefully we can extend this strategy to other parts of our app to eventually have a fully React web app. That’s all I got for now. Until next time, happy coding!