Hello Habr! I am Milos from Badoo, and also this is my very first Habr post, initially posted inside our technology web log. Hope you http://www.besthookupwebsites.net/escort/inglewood enjoy it, and please share and remark for those who have any queries
So… React, amirite.
Dozens of accomplishments, without also being a framework.
Right Here when you look at the Cellphone online group, we don’t follow any strict JS frameworks – or at the very least, any popular people – therefore we work with a mix of legacy and technologies that are modern. Although that actually works well we wanted to alleviate this by reducing the number of «manual» updates, increasing our code reuse and worrying less about memory leaks for us, manipulating DOM is usually hard, and.
After some research, respond had been considered the choice that is best and then we chose to opt for it.
We joined up with Badoo in the exact middle of this procedure. Having bootstrapped and labored on React projects previously, I became conscious of its benefits and drawbacks in practice, but migrating an adult application with billions of users is a very different challenge|challenge that is completely different.
Our own HTML files had been well organised, & most of y our rendering had been done since just as template.render() . Just how could we retain this simplicity and order while going to respond? For me, technical problems apart, one concept ended up being apparent: replace our current telephone calls with JSX rule.
After some planning that is initial provided it and wrapped up a command-line tool that executes two easy things:
- Substitute template.render() calls aided by the HTML content
needless to say, this could just go us halfway, because we would nevertheless need certainly to alter the rule manually. Considering the amount and wide range of our templates, we knew that the approach that is best will be one thing automatic. The initial concept sounded not so difficult — and it can be implemented if it can be explained.
After demoing the first device to teammates, the most readily useful feedback ended up being that there’s a parser available for the templating language we used. Which means we could parse and convert rule much simpler than we’re able to with regular expressions, for instance. That’s whenever i truly knew that this could work!
Lo and behold, after a few times an instrument came into existence to transform Dust.js HTML-like templates to JSX React rule. We utilized Dust, however with a thorough option of parsers, should really be comparable for translating some other popular templating language.
For lots more details that are technical skip towards the Open-source part below. We used tools like Esprima to parse JS rule, and a PEG.js parser generator to parse Dust templates. Into the really easiest of terms, it is about translating this sort of template rule:
to its JSX code equivalent:
See side-by-side comparison right here.
following this, our procedure ended up being pretty straightforward that is much. We immediately converted our templates from a single structure , and every thing worked not surprisingly (many thanks, automatic evaluation). To start with, we preserved our old template.render() API to help keep modifications isolated.
Needless to say, by using this approach you nevertheless get templates rather than “proper” React components. The genuine advantage is in the undeniable fact that it is much easier, if you don’t trivial, to respond from templates which can be currently JSX, more often than not by merely wrapping a template rule in a function call.
You may think: you will want to compose templates that are new scratch rather? The brief response is that absolutely nothing incorrect old templates — we merely had lots of them. In terms of rewriting them and dealing towards real componentisation, that’s a story that is different.
Some might argue that the component model is simply another trend that might pass, why agree to it? It’s hard to anticipate, but one feasible response is which you don’t need certainly to. Until you find the format that works best for your team if you iterate quickly, you can try out different options, without spending too much time on any of them. That’s certainly one of the core concepts at Badoo.
Within the character of performing something well, we’ve built these interior tools in a few components:
- dust2jsx — package accountable for real Dust to JSX interpretation
- ratt (respond all the stuff) — command line device for reading/writing files on disk. accountable for including referenced templates, and utilizes dust2jsx internally to transform rule
We’ve even open-sourced these tools — make sure to always check them down, and also other open-source materials on our GitHub web page. Please add keep us a remark if you learn them helpful.