WhoBuilt

React Native, The Native Bits

Sep 14, 2018· 30 min· 2.3K views·Watch on YouTube

Speaker

Emil Sjölander, a C developer on Facebook’s Native UI Frameworks team in London, explains how React Native turns JavaScript and JSX into Android views at React Native EU.

React Native crosses several native layers

Sjölander notes that JavaScript makes up less than 40% of React Native’s code; the framework also uses Java, C, C++, and Objective-C on its supported platforms. He focuses on Android, where the framework uses JavaScript, Java, C++, and C, and says apps pass through at least four runtimes and languages before showing UI.

React commands build the shadow tree

React compares UI trees and produces imperative commands describing what changed. On an initial render, UI manager messages create shadow nodes and connect them; for example, text uses a virtual text node inside an RCTText node. These native-side commands build a mutable shadow tree from declarative JSX.

Yoga calculates layout across Java and C

Yoga is a portable C layout engine that implements a mobile-focused subset of CSS, mainly Flexbox. React Native creates Yoga nodes for layout-relevant shadow nodes, then calls YGNodeCalculateLayout to determine positions and sizes. Text measurement calls back into Java through a measure function, and styled text nodes do not need separate Yoga nodes.

Android removes views that draw nothing

View managers create and update native views and shadow nodes for types such as text and inputs. On Android, the native view hierarchy optimizer skips nodes that only affect layout and draw no pixels, such as uncolored row containers in a Flexbox grid. In Sjölander’s example, four shadow nodes become three Yoga nodes and two Android view nodes.

Updates reuse trees and cached measurements

Press handling can send direct commands to an existing view, such as setting its pressed state and ripple location, without going through the shadow and Yoga trees. After the press handler adds “Hello React Native EU,” React sends incremental commands that mutate the existing trees. Yoga reuses cached text measurements, while the Android hierarchy is updated to add the new text view.

Takeaways

  • Trace React Native UI work across the JavaScript, shadow-tree, layout, and platform-view layers when diagnosing performance.
  • Use Yoga for layout so Android views are not responsible for measuring and positioning every node.
  • Keep Android view hierarchies lean by avoiding native views for containers that draw no pixels.
  • Incremental updates and cached text measurements can reduce repeated layout work.
  • Use direct view commands for interactions known not to change layout, such as a button’s pressed state.

Mentioned in this talk

Search inside this talk

What was claimed 7

Related talks