In most React Native apps the business logic runs on the JavaScript thread: the React application lives there, API calls are made there and touch events are processed there. One long piece of work on that thread can therefore make a button feel dead and a JavaScript animation stop mid-move. Knowing what waits on the thread tells you what will freeze, and which work to push out of the frame that handles a tap.
Key terms
- JavaScript thread: the thread where the React application lives, API calls are made and touch events are processed.
- requestAnimationFrame: a browser-style timer global that React Native provides, paired with
cancelAnimationFrame. - requestIdleCallback: a global that schedules a callback for when the JavaScript thread is idle.
- babel-plugin-transform-remove-console: a Babel plugin that removes every
console.*call from the production versions of a project.
Everything that waits on JavaScript
Updates to native-backed views are batched and sent to the native side at the end of each iteration of the event loop, before the frame deadline when all goes well. If the JavaScript thread is unresponsive for a frame, that frame counts as dropped, and these are the symptoms while it stays blocked:
- Any animation controlled by JavaScript freezes.
- Touches go unanswered. The main thread sends raw touch events over, but a busy JavaScript thread can't process them, so a
TouchableOpacitycan't tell its native view to change opacity. - JavaScript-driven screen transitions slow down. Slow Navigator transitions are the most common sign of too much work on the JavaScript thread at once. Native stack navigators run their transitions on the main thread, so they keep going.
TouchableOpacity is a good example of how far the dependency reaches. Its fade to activeOpacity (0.2 by default) runs with Animated.timing and useNativeDriver: true, yet the animation starts from the component's JavaScript onPressIn handler. The dim can't begin until the JavaScript thread picks up the touch.
Letting the press feedback land first
A common case starts with an onPress that sets state and triggers a heavy re-render. A few frames drop, and the opacity or highlight change on the pressed component doesn't show until onPress has returned. The fix is to wrap the action inside the handler in requestAnimationFrame.
The ReportScreen component in the example shows the pattern on a button that reveals an expensive report. Its handler schedules the state update instead of making it directly:
tsxfunction handlePress() { requestAnimationFrame(() => { setShowReport(true); }); }
The re-render still runs on the JavaScript thread and costs the same. What changes is when it starts: the callback is scheduled as a separate timer, so the handler returns and the press feedback is sent before the deferred update runs.
Both timers are globals, so there is nothing to import. The callback receives a numeric time, the value of performance.now(), and the call returns a numeric handle for cancelAnimationFrame. requestAnimationFrame currently schedules its callback like setTimeout(fn, 0), and this may change, so don't count on it waiting for a frame boundary.
Deferring work until the thread is idle
For work that can wait longer than the next frame, for example during a screen transition, defer it until the JavaScript thread is idle with requestIdleCallback. The global takes a callback that receives a deadline object with didTimeout and timeRemaining(), accepts an optional timeout option, and returns a numeric handle.
console calls in release bundles
In a bundled app, console.log statements can be a big bottleneck on the JavaScript thread. That includes calls from debugging libraries such as redux-logger, so remove them before bundling.
babel-plugin-transform-remove-console does the removal at build time. Install it with npm i babel-plugin-transform-remove-console --save-dev, then add the .babelrc from the second example to your project directory. It lists transform-remove-console under env.production.plugins:
json"plugins": ["transform-remove-console"]
With that config, every console.* call is removed from the release (production) versions of the project. Add the plugin even if your own code never logs, because a third-party library can make console calls too.
Code Examples
import {useState} from 'react';
import {Text, TouchableOpacity, View} from 'react-native';
import {YearlyReport} from './YearlyReport';
export function ReportScreen() {
const [showReport, setShowReport] = useState(false);
function handlePress() {
requestAnimationFrame(() => {
setShowReport(true);
});
}
return (
<View>
<TouchableOpacity onPress={handlePress}>
<Text>Show yearly report</Text>
</TouchableOpacity>
{showReport && <YearlyReport />}
</View>
);
}{
"env": {
"production": {
"plugins": ["transform-remove-console"]
}
}
}