Christopher Chedeau on the Core Philosophies that Underlie React
Published: 2016-01-11 15:41:39

<p><span style="font-weight: 400;">The&nbsp;<a href="https://facebook.github.io/react/" target="_blank">React</a> project at Facebook gained a lot of popularity in the past year, with its to main Github repositories earning a combined 60,000 stars. And while React was initially open-sourced in 2013, it was not until last year, along with the announcement of React Native, that the project's popularity took off. </span></p>
<p><span style="font-weight: 400;">With its take on declarative user interface building and the excitement of better mobile development experience using <a href="https://facebook.github.io/react-native/" target="_blank">React Native</a>, project has lit a fire in the JavaScript community and influenced coding practices by directly challenging established coding practices and conventions. At the center of the project has been&nbsp;<a href="https://twitter.com/vjeux" target="_blank">Christopher Chedeau</a>, a front-end engineer at Facebook.&nbsp;The Practical Developer had some burning questions about the future of React.</span></p>
<h2>How has React Native affected ReactJS and vice versa?</h2>
<h3>Rendering Targets</h3>
<p>One thing many people are confused with is that React Native is not a reimplementation of React for Native. React Native actually uses the same exact React code under the scenes. React Native provides a different set of primitive components. Instead of having <code>&lt;div&gt;</code> and <code>&lt;span&gt;</code> as low level primitives, it has <code>&lt;View&gt;</code> and&nbsp;<code>&lt;Text&gt;</code> which are baked by their respective counter parts in iOS and Android.</p>
<p>React Native is not our first attempt to target something other than the DOM with React. We had a charting library called&nbsp;<a href="https://github.com/reactjs/react-art" target="_blank">ReactART</a> targeting canvas, svg or vml based on the browser capabilities. While it was certainly possible to do so, it would require some hacking. Once we were able to prove that React Native could work, we initiated a proper separation between React internals and the renderer. We now have React, React-dom and React Native.</p>
<p>What's really exciting is that this helped external people write other renderers for React. There are active projects targeting 3D and virtual reality, terminal and even hardware such as LEDs.</p>
<h3>Performance</h3>
<p>One of the biggest concerns we had when we started the project was around performance: could React and JavaScript compete with native in terms of performance? What we found is that it is indeed possible to write apps using React Native with great performance.</p>
<p>The big takeaway is that the difference between mobile and web is around performance characteristics. On web, you want to minimize the total time it takes to render and update things, which is where React does a great job. On mobile, you need to respond within a fixed amount of time.</p>
<p>Everyone is talking about 60fps for a reason. You typically have a 16ms per frame limit to respond to a user touch interaction or animation. If you don't meet this deadline, the user will likely notice.</p>
<p>We've been approaching this problem on many fronts. First, architecturally, we are using a pipeline architecture where the ui thread talks to the js thread, which then talks to the layout thread, which next talks to the "React Native core" thread, which finally sends operations to the main thread and repeats. This approach enables us to use the multiple cores that phones have.</p>
<p>JavaScript is still single-threaded, so we are looking at ways to better schedule operations. We use InteractionManager to avoid doing computationally expensive work while an interaction or animation is running,&nbsp;<a href="https://w3c.github.io/requestidlecallback/" target="_blank">requestIdleCallback</a> to schedule work at the end of the frame before the next one kicks in, and an animation API that's both expressive and can be ran outside of the js thread -- splitting up the React rendering work over multiple frames.</p>
<p>The other big area we invested in is tooling. Unfortunately, there is not a silver bullet that's going to make your app run faster, most of the time the best way to make an app run fast is to profile it, find the bottleneck, aggressively optimize it and repeat. Once you've done that, you need to have a continuous performance testing to find and fight regressions that are going to creep in. In order for those to happen, you also need to change the engineering culture to care about those. None of this is React Native specific. We are using these learnings for the web side of things as well.</p>
<h2>The sprawl of platforms and conventions causes a lot of development pain that React&rsquo;s abstractions remedy. How do you expect this to unfold over the foreseeable future?</h2>
<h3>Problem</h3>
<p>When I started working on React Native, I thought that there were fundamental differences between iOS, Android, and the web. However, the more I re-implemented things for React Native, the more I realized that if the naming and languages are ignored, they still perform the same functions in very similar ways, but are incompatible between each other.</p>
<p>You can see this if you are looking at popular apps: developers want to do the same things for the three platforms baring small design differences. It&rsquo;s unfortunate that in order for developers to build on mobile across three platforms, you need three different engineers because there are three different sets of tool chains, languages, and API&rsquo;s.</p>
<h3>The React Native take</h3>
<p>I recognized the aforementioned problem over two years ago but didn&rsquo;t feel confident that it would be fixed anytime soon. I considered React Native an ambitious project that was designed to solve a problem that shouldn&rsquo;t have existed in the first place.&nbsp;There have been countless projects that attempt to resolve the cross-platform issues but none of them bring any fundamental value to the table. So, you might get your hands on a new platform, but quickly learn that it&rsquo;s very similar to what you previously used, does not support multiple platforms, and introduces a variety of issues trying to interop with both existing platforms.</p>
<p>React Native was originally referred to as &ldquo;React iOS&rdquo; for most of its existence, but wasn&rsquo;t platform-agnostic. I wanted the project to succeed because it is better and not because management forced developers to use it due to the cross-platform feature.&nbsp;This was a very difficult challenge as UIKit and Xcode set the bar really high.</p>
<p>The first big differentiator is React, which is a fundamentally different way to build UIs. Interestingly, we are now building our native iOS app using the ideas we used with React and the obj-c framework called&nbsp;<a href="http://componentkit.org/" target="_blank">ComponentKit</a>.</p>
<p>The second is the emphasis on the developer experience itself. We provide a much better developer experience by shortening the edit-reload cycle using the interpreted JavaScript programming language and a newly built packager tool built for speed. We also provide much needed tools such as red and yellow box to present errors in a better way, an element inspector to quickly find the piece of code that generates the UI sections, Chrome console, debugger, and network panel.</p>
<p>The thrill of the project is that the benefits of React Native extend beyond what I previously highlighted, as the open source community embraces it into their own environments. Our open source approach is important because we&rsquo;re helping each other be better and most importantly create new standards in today&rsquo;s complex technology environments.</p>
<p>Our goal is to provide a strong set of API's for other companies to embed as a React Native view inside of an existing application. If you end up realizing that you want to go back to native, you can gradually step out by writing your new views in pure native.</p>
<p>With that said, there are a number of companies with different strategies and challenges to face. While the future of what exactly this will look like is unclear, I&rsquo;m hopeful that focusing on the developer experience while maintaining an open and transparent environment will continue to be a priority.</p>
<h2>React has illuminated a debate about &ldquo;separation of concerns&rdquo; and it seems like you personally lit a lot of the fire around &ldquo;CSS in your JavaScript&rdquo;. How has the community reacted, and how have these reactions evolved the way the core team thinks about these things?</h2>
<p>I think that "separation of concerns" is mostly a way to explain and rationalize the template/JavaScript/CSS architecture. If you write your code that way, then it makes sense to talk about "separation of concerns".&nbsp;But this is not the only way to build applications. If you look at the iOS environment, it doesn&rsquo;t have style sheets or templates, and yet write applications just fine.</p>
<p>The reason why React and React Native do it differently is best explained by looking at the history.&nbsp;Facebook front-end has historically been written in PHP -- where you would build your HTML with echo and string concatenation. However, there are security concerns associated with this approach because if for some reason you forget to escape user provided data, you can inject arbitrary code that will be executed.</p>
<p>A clever engineer solved this issue by promoting HTML as part of the PHP syntax. Writing echo <code>&lt;div&gt;{$text}&lt;/div&gt;</code>&nbsp;is valid and automatically escapes the <code>$text</code> value. Not only did it solve the security issues related to string concatenation but it also opened up the ability to build custom components, enabling you to build the entire website mixing HTML and code. We then ported the idea to JavaScript and it worked equally well there.</p>
<p>For styles in JavaScript, I neglected to implement a CSS parser and rule evaluation engine (I already rebuilt the layout part of CSS). I was convinced that I would need to do it at some point but delayed it as much as I could.</p>
<p>It turns out that this was a good decision. Not only did we not need CSS, but it also solved a ton of issues that we had with CSS at Facebook.&nbsp;<a href="https://vimeo.com/116209150" target="_blank">I presented at NationJS</a>&nbsp;as an attempt to address the controversy associated with JavaScript styles and to help provide some educational background before announcing React Native a few months later.</p>
<p>Once we did announce it, I had absolutely no idea that React Native would trigger such a wide movement of people trying to find different ways to define styles. Our inspiration came from the post-launch discussions and internal prototype developments on the web side of things &mdash; the React Native approach is a bit too radical in the web environment.</p>
<h2>How does React fit into Facebook&rsquo;s greater development philosophy?</h2>
<p>We are part of an organization at Facebook called product infrastructure. Our mission is to create ways to move faster, iterate with confidence, and build more reliable software.&nbsp;With that said, every bit of software we use today is made with other software that has already been built. We build software tools to build software and more importantly, we incorporate open source software into our existing software. So, the way in which we build software today has been developed over decades leading up to today.</p>
<p>React has influenced a number of other frameworks, including and excluding the UI, and has pushed us toward a more functional, declarative, and asynchronous solution &ndash; away from imperative paradigms.</p>
<p>When we have a potential solution, we leverage real projects that need support and iterate on it with our solution because we know that to deliver software with the speed and quality we want, we will have to rethink everything about how we build.</p>
<h2>More of the developing world gains internet access.&nbsp;What is the dialog like at Facebook regarding how these projects can serve the needs of an evolving world while also providing stability to the developers adopting and working on the technology?</h2>
<p>Facebook has a very interesting characteristic in that the product is universal. Outside of translation, people all over the world have access to the same product.</p>
<p>The challenges of making the world more open and connected that our engineers are helping to solve create the same experiences that I was able to access as a Frenchman in Paris.</p>
<p>The biggest difference isn't around how people want to use Facebook but the technology they are using to access it. The computing power of the phones and network connections are more challenging in emerging countries. This is actually a positive situation to be in as an engineer at Facebook because making the app work well under challenging conditions requires creative thinking. If you can make Facebook work better on bad network conditions, it usually will also work better on stronger network conditions; improving many global user experiences at once.</p>
<h2>Are there any Facebook projects outside of the React universe that people should be talking about more?</h2>
<p>Related to the front-end world, the two I have in mind are&nbsp;<a href="https://github.com/facebook/rebound" target="_blank">Rebound</a> and <a href="https://github.com/facebook/pop" target="_blank">Pop</a> which aim to write animations related to gestures more easily. I was greatly inspired by them when writing the Animated API on React Native.</p>
<p>If you are into static analysis, <a href="https://github.com/facebook/hack-codegen" target="_blank">Hack</a>, <a href="https://github.com/facebook/flow" target="_blank">Flow</a> and <a href="https://github.com/facebook/infer" target="_blank">Infer</a> are projects to keep tabs on. The traditional way for people to work on those was to create a new language from scratch with those properties. Being able to augment an existing and popular language with a type system and use it at scale is a very exciting prospect. 😀</p>
<p><a href="https://github.com/facebook/osquery" target="_blank">OSQuery</a> is a very unusual project coming from our security team. It enables you use SQL to query running processes, open network connections, etc. This is a big improvement over having to parse the text output of various Linux commands and is a good example of improving the developer experience.</p>
<p>I am continuously inspired by the ongoing and unique ways we&rsquo;re able to improve tooling via open source projects and I really hope to see more collaboration in the community continue.</p>