Yellow marks are automatic key passages. They aren’t personal saved highlights. Select a timestamp to follow along with the video.
React JS is a conference talk by Tom from Facebook that traces React's origins, from its birth in the ads organization and its first use on facebook.com to its open sourcing at JSConf in 2013. The talk argues that declarative, component-based code is far more predictable and maintainable than the mutative, imperative approach that preceded it, and that this structure is what gives React its power. It then turns to React Native, which brings the same model to iOS and Android through an asynchronous, batched JavaScript layer, and it closes with live demos of Facebook's Groups app and the idea of "learn once, write anywhere."
Welcome and Housekeeping
Good morning, everyone, and welcome to React JS. The organizer of the conference opens the event with a few announcements. Unusually, attendees drop off their badges when they leave on the first day; the badges are returned the next morning and can be kept after that. Restrooms are near the registration desk. Anyone who needs to smoke or get fresh air can leave by the door they came in through. Anyone who needs help should look for the people in flashy green T-shirts. Tom is then invited to speak about React.
Introduction: Building Products at Facebook
Tom thanks Christopher, noting that he is probably as excited as the organizer, who put a great deal of work into the event along with Christina. Tom has been at Facebook for a while and has worked on many things. He used to build products, including the search typeahead and the input used when mentioning someone on Facebook. These days he manages the React team, so talking about React is fitting. First on the agenda is where React actually came from. Every week there seems to be a new podcast or interview telling how React started, and dozens of stories are floating around, so this is one perspective on its origins, and a good way to open the first ever React JSC.
The Problem: Complicated Client-Side MVC Apps
React was born in Facebook's ads organization. The team was building client-side applications in a traditional client-side MVC style, with two-way data binding and templates, the same approach familiar to most developers. Views listened to changes on models and responded by manually updating themselves. These apps started out simple, but as more features and more people were added, they grew more complicated and the company began moving more slowly on these very large code bases. Over time the apps became a nightmare to work with.
Cascading Updates and Unpredictable Code
When new data flowed in, a small change somewhere deep in the tree could effectively force the whole app to render, or force the code to figure out what needed to change and mutate many different views. Internally these were called cascading updates. They were slow, because the bookkeeping to work out what needed to be mutated was followed by the work of updating every view, and much of that work was wasted. Over time, engineers could no longer hold in their heads what would trigger a cascading update. The code became unpredictable, confidence in new code dropped, and nearly the whole application was rendered on any change anyway.
The Insight: Re-Execute the Description
The team realized that they had already written code describing what components, and whole applications, look like given an initial set of data. When the data changes, the same code could simply be executed again. This has obvious problems, but conceptually it is much simpler. The problems are user experience glitches and inefficiency: if a user selects some text or has state in the DOM, that state is lost and things flicker, and a huge amount of time and CPU is wasted on the client. It was not that bad in practice, though. For a long time the chat buddy list on facebook.com worked exactly this way: whenever a buddy came online or went offline, a new string of innerHTML was generated and dumped into the DOM, and no one noticed. It worked well, and it was simple to understand.
The Birth of React
The mental model was right: describe what views look like given any set of data. What remained was to fix the poor user experience and performance in certain cases. Jordan Walke built a prototype to make this process more efficient and give a reasonable user experience. It had no name yet, but that is when React was born. People began playing with it internally, and everyone had the same reaction: they had no idea how it would be performant enough, but it was so much fun to work with that nobody cared whether it was too slow, because somebody would make it faster.
React on facebook.com: Like and Comment
A few months later, while apps were still being built in the ads organization, the first React feature on facebook.com, the user-facing site, was built: the like and comment experience. It replaced a previously server-side rendered version with one powered completely by React and Flux. To the team's surprise, metrics were neutral to positive at launch. The result was a code base that was significantly more maintainable and significantly more fun to work with, and React was on facebook.com.
A Complicated Ads Flow
In parallel, the ads products were being built. A screencast of the Advertiser create flow shows how complicated some of these apps were compared with the simple like and comment experience. On the right is an audience definition; on the left is a form for adding demographics that determine who can see the ad. As cities are added, the maximum estimated reach is computed in real time; three cities with their radiuses, for example, give a potential reach of 7.8 million people. The age range, interests and much more can be changed. Even partway down the page, the form is only about halfway through, and with a traditional MVC approach, laying out models and views, there would already be probably several dozen models. The view on the right is fed by so many different models that writing the code to manage it was insane. Built with traditional MVC, the application was very hard to build and broke all the time, because nobody knew how to change it and everyone was afraid to try. Jason Bonta later in the day gives a presentation on some of the ads interfaces and how they are made fast.
Instagram and Open Sourcing React
Around the same time, Instagram joined Facebook with several projects, one of the first being instagram.com. They looked at Facebook's stack and liked React. The problem was that React was tightly coupled to Facebook's specific software stack, so it had to be decoupled for Instagram to use it. Pete Hunt convinced the team that this was worthwhile and did most of the work of making React open-sourceable. At JSConf in 2013 the team decided to open source it. First impressions were skeptical. Attention held until the slide showing JSX, at which point the audience tuned out, saying they had never seen anything like it, and took to Twitter. Their criticism was that embedding markup inside JavaScript was a huge step backwards for the maintainability of code, which seemed crazy to them.
Why the Syntax Was Not Controversial: XHP
Internally the syntax was not controversial at all, since the team had been using something similar for years. About four years earlier, XHP had been introduced into the PHP stack, and it was open sourced in 2010 as well. It was simple: XML fragments written inside PHP code were desugared into function calls. It brought many benefits. At the time the most important seemed to be that it made cross-site scripting attacks a thing of the past, since the system knows where each piece of content is rendered and whether it is user-facing, so it can escape the content automatically and engineers no longer need to type HTML special characters.
Composite Components and Separation of Concerns
The most important benefit of XHP was that it allowed composite components. An FB Jewel, the little icons at the top of facebook.com that are clicked for notifications, did not just send down the markup needed to render it, but also all the style and presentation information and all the JavaScript that made it do something. Components could also take very simple parameters, such as a viewer and the type of data wanted, and do all the necessary data fetching, so a single API call returned a fully functional, dynamic component with all its data. Behind the scenes there was parallelized data fetching across components, caching layers and CDNs, but a client of the component did not have to think about any of that; the implementation did not matter. This is a component, and it is regarded as the proper separation of concerns for applications. The syntax was brought into React because it was more composable and more approachable for people who had seen XHP before. Declaring views in JSON, by contrast, means many curly braces and parentheses, whereas with XML tags you actually see the closing
Closing tags in JSX syntax
With XML-style tags, the closing tag appears on its own closing line. That was the main reason the team adopted the syntax. Declaring views with JSON requires tracking many curly braces and parentheses, while the tags make each element's structure visible.
Reactions to the 'rethinking best practices' tweet
Facebook was once accused of rethinking established best practices simply because it stored its markup in JavaScript. A tweet from Ben making that point was somewhat sarcastic at the time. The team has had many conversations about it since, but they appreciated it, feeling that someone was finally acknowledging that they were rethinking how everyone builds things. Not coincidentally, Pete gave a talk at JSConf titled 'Rethinking Best Practices.' The talk covered many of React's fundamental principles and the qualities that made it exciting and powerful. Most of the audience has likely seen it.
Features credited for React's power
If each person in the room were asked individually what makes React powerful, the answers would likely vary. The most common answer is probably the virtual DOM, React's claim to fame and the feature that initially set it apart. Standalone virtual DOM implementations now exist, and other JavaScript frameworks have adopted the virtual DOM, which is a positive development. Many people also cite server rendering as React's most powerful feature. Bonnie from Codecademy recently said that Codecademy chose React because server-side rendering worked out of the box so easily.
Descriptive warnings and the declarative API
Developers using React for the first time often name its descriptive warning messages as the most powerful feature. When something goes wrong, React reports exactly what the mistake was and how to fix it, pointing to the line of code that needs attention. Other notable features include the custom event system and many more. What makes all of these features and more possible is that React replaces an imperative, mutative API with a declarative API that favors immutability.
Recognizing the importance of declarative code
Internally, the team did not initially understand the significance of this. It was not until they built many products on React, and then had to change and extend those products over time, that they realized the operative word was declarative. When applications and views are structured declaratively, the code becomes significantly more predictable.
Predictability and confident changes
Any JavaScript file with a React component can be opened, and its possible states and behavior can be understood just by reading it. This is incredibly powerful. Because declarative code is predictable, changes can be made more easily and with confidence. Engineers do not need to spend a lot of time understanding the entire system before changing one small piece of it.
Onboarding engineers at Facebook
Facebook experiences this every day. Engineers who have never worked with React join teams building React applications, and on their first day they can make changes to the codebase with confidence. The team considers this remarkable, since they have never seen anything like it before.
Reliability and business impact
Being able to change code confidently also makes it more reliable. Facebook's ads creation flow used to break multiple times a week. After it was converted to React and Flux, it never went down again. The team did not slow down. It sped up, adding more features in less time with more engineers than was possible before, which has had a meaningful impact on the success of the business. React's real power lies in how it makes developers structure their code in terms of components.
Components as the building blocks of applications
The virtual DOM, and conceptually re-rendering on every update, is only possible because Facebook structures its code with React components rather than forcing things into models, views, and controllers. All concerns are separated into components, which are the fundamental building blocks of the application. In a conversation with Jay Longster, the discussion turned to what makes React components different from components in other frameworks.
What sets React components apart
Many frameworks, including those with directives and web components, have also recognized that components are the right separation of concerns. In Facebook's experience, the difference is that other systems require creating components with imperative, mutative code, while React lets developers describe components declaratively. Declarative code is easier to maintain and extend, and it is also more enjoyable to work with. Facebook uses React everywhere internally, and everybody on the team loves using it. The problem is that React has only been used for web projects.
The limits of wrapping web apps for native platforms
Facebook also has iOS and Android apps with hundreds of millions of users. In the past, the team tried building web apps and wrapping them in thin native wrappers, but this did not work. A web app cannot be expected to behave like a native app, because building for the native platform yields a vastly different user experience than building for the browser.
Why Paper is hard to match on the web
Many people in the audience have used Facebook's Paper app, and it feels amazing. It is probably the best-feeling iOS app available. Its feel comes from many reasons, and people have tried to clone it on the web, but no one comes close. A clone might copy one feature or gesture, even with a huge codebase, but nothing feels as good or is as complete as Paper. The question is why web technologies cannot be used to build something like Paper.
Threading limits on the web
The first reason is that native platforms can parallelize work across multiple threads. Facebook built a framework called AsyncDisplayKit, which moves rendering off the main thread and enables very smooth animations. Browsers have web workers, and some people suggest using them to parallelize work, but web workers are a severely limited API. Decoding cannot be done in a web worker unless one cross-compiles code, and Pete Hunt tried this and advises against it. Text measurement and layout also cannot be done there. The web therefore lacks the true power of multiple threads.
Gesture handling and native widgets
Native platforms also have sophisticated gesture handling. The web has the raw materials, such as touch events entering the system, but it lacks the proper tooling and the developer discipline needed to build an advanced gesture system as sophisticated as the native ones. Native widgets are also a black box. Developers do not have access to their source code, and they cannot see the parameters or numbers that make the widgets feel the way they do. When someone reimplements a native widget with HTML, CSS, and JavaScript, the result may look close, but it is rarely indistinguishable and is often not close at all.
Platform-specific access and flexibility
Native platforms also provide access to things that web developers cannot reach. For example, Apple's map data can only be accessed through MapKit on iOS, so a map implementation on the web cannot look and feel like the native map component. Native developers may choose to reimplement widgets, but web developers are forced to. Although it is painful to admit, the native environment is far more flexible and powerful than the browser environment, at least today.
The costs of native development
All of that power and flexibility comes at a cost. The native environment is generally far more hostile to developers and organizations. When Facebook started building separate iOS and Android apps, it had to set up two sets of tooling, two sets of abstractions, and a large amount of platform-specific code and infrastructure, and none of it was portable between teams. Developers must also manage memory, thread concurrency, and subview layout, as well as provisioning profiles for devices and a deployment process. Developers can no longer just load a browser window or push their entire application with a single command. This adds overhead to building applications.
Imperative APIs in native development
Native development also relies on mutative, imperative APIs, just as the DOM does. Whenever the team has tried to build sophisticated applications with these APIs, the work has broken down over time as the applications became more complicated and more people joined the team.
React Native as a solution
React has been designed to work with any view system and can wrap any imperative view system, not just the DOM. What if the same React JavaScript that runs on the web could power truly native applications? That is exactly what the team has been working on. It is called React Native, and it is being built simultaneously for iOS and Android. The presentation focuses on React for iOS, and the announcement feels like a Steve Jobs-style moment.
Demo format
The presenter offered to show a prerecorded video or to demonstrate live on the phone, and the audience chose the live demo.
Movies app demo
The first demo is a very simple movies app. It lists movies, and tapping any movie shows a detailed description, with an option to go back. Users can also search for movies, with examples such as Furious 7 and Furious 6. Two native details are less obvious. Tapping the native status bar scrolls to the top, which is native behavior. Swiping uses the native navigator, since these are native components.
Facebook Groups app and a hybrid challenge
The next app is Facebook's Groups app, which is available in the App Store so the audience can download and try it. It combines native code and React Native code. The audience is challenged to identify which parts are which. The demo opens the React.js group and a photo, which can be zoomed and flipped.
Liking a post in the demo
The demo continues in the React.js group, where the presenter likes a post, showing the app's interactions in action.
More of the Groups app demo
Another group in the app offers more to try. A multi-photo post lets the user swipe between photos and flick them away, and previous comments can be viewed. Focusing the text box and then going to the top of the post leaves the feed behind: the screen is now a permalink view. Posting a comment works. Liking a post that had no likes before triggers an animation in which the first like jumps in, a detail the presenter particularly enjoys.
Everything on screen was native
Back to the presentation. The first and most important point is that everything on the screen was completely native. The web cannot be used to build an experience like that, a fact learned from many attempts. There was no HTML, no browser and no web views, yet the whole thing was controlled by JavaScript.
Other frameworks exist today that script native APIs in JavaScript, but wrapping native has a problem: developers still have to use imperative, mutative APIs. The only thing that changes is the language the code is written in.
The danger of a naive JavaScript layer
Introducing JavaScript naively could end up being a lot worse. Frames could be dropped in the UI because the app is waiting on JavaScript to finish some work. JavaScript might be running garbage collection, for example, while the main thread drops frames. A JavaScript layer therefore has to be introduced carefully.
An asynchronous, batched design
The layer needs to be asynchronous, so that anything can happen on the JavaScript thread without affecting rendering of the UI and without blocking it. For performance, all the instructions to be executed on native should also be batched up and run whenever possible, yielding to the main thread whenever necessary.
React diffs successive versions of the virtual DOM, which makes it a perfect fit. The diffs produce sets of instructions that are thrown into a task queue and then executed on native. On the web, those instructions become imperative DOM calls such as set-attribute calls.
Mapping to native primitives
There is no reason to run under the DOM, though. The web has a set of primitives of divs, spans and images. Native platforms have different ones: UI views, text and images on iOS, and Android views on Android. The task queue is interpreted, along with all the changes that need to be made. Where it once meant "set attribute ID this" or "set color that", the changes are now turned into imperative native calls.
Running app logic off the main thread
This is where much of the work for React Native went: thin wrappers convert declarative properties into imperative code. Probably the coolest part is that multi-core architectures can be exploited much better. Putting the JavaScript in a background thread means all of the application logic, including rendering, always executes off the main thread.
This is what makes Paper, built on AsyncDisplayKit, so powerful, but with Paper the only thing happening off the main thread is rendering, and in native the app has to be restructured in a number of ways. With this architecture, all application code, all of the JavaScript, runs off the main thread by default. Just as when React was introduced a couple of years ago, this is working in production. It is in the App Store in Facebook's Groups app, with more coming soon.
Not a single codebase
Some people think that, with this tool available, Facebook will revert to a single codebase and build all its apps with it. That is not the goal. The aim is not a single codebase powering every application and platform, and not the "write once, run anywhere" pipe dream. Platforms have different looks, feels and behaviors, and someone building an Android application thinks about Android principles, not iOS principles.
Learn once, write anywhere
The goal is instead "learn once, write anywhere", a phrase credited to Nick Schrock. The aim is a consistent set of tools and technologies for building applications on the same principles across any platform. A set of engineers can build an iOS app and, the next day, build the exact same app for Android simply by knowing React. They need no special IDEs or special environments; they can just build using React.
Code demo and open source plans
Tomorrow morning, Christopher Chedeau will give an actual coding demo and change some things live. There is no time to get into code now, with only 13 seconds left, but a further 30 seconds go to where the code will be. It will be open sourced soon, and everybody at the conference will have access to the GitHub repo tomorrow. It is still pretty bare bones, but more is being added every day, with features dropping and landing daily.
A story from before React was open sourced
A very quick story to close. Before React was open sourced, Sebastian remarked, somewhat amusingly, "Jordan, you know that people are going to hate this, right?" and Jordan's reaction was "man, what the heck?" The message to the team was that the release is very exciting for everyone, but what happens next is even more exciting.
React's influence on how JavaScript is thought about
What happened next is known: React fundamentally changed the way many people think about JavaScript application structure. Simply because React was released, many other JavaScript frameworks took notice and were influenced. As a rough estimate, the number of frameworks influenced by what React is doing is somewhere around all of them.
It has also changed how people think about JavaScript. A couple of years ago everything was about Object.observe, but now the community is saying that immutability is actually pretty interesting and perhaps worth focusing on. The presenter is excited that the community continues to rethink best practices, and thanks the community for making it possible to keep investing in and working on this. Applause.
Turn your watchlist into a reading routine
Bring your YouTube videos together, read summaries with key passages marked, and explore the full transcript. Save your own highlights with Pro.
Start with 5 free batteries. Each covers up to 30 minutes of a new summary.
New summaries depend on video availability and demand; processing may take time or require retry.