Defer Heavy Work with InteractionManager for Smoother Animations
Learn how to use React Native's InteractionManager to postpone expensive calculations until after UI interactions, keeping animations buttery smooth.
Insight
When a screen transition or animation is in progress, any synchronous JavaScript work can jank the UI. React Native’s InteractionManager lets you schedule heavy tasks to run after all ongoing interactions finish. By off‑loading data parsing, layout calculations, or state updates until the UI is idle, you keep frame rates high and avoid dropped frames.
Example
import { InteractionManager, useEffect, useState } from 'react';
import { View, Text, ActivityIndicator } from 'react-native';
export default function DetailScreen() {
const [data, setData] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
// Simulate fetching raw JSON then heavy processing
fetch('https://example.com/large-payload')
.then(r => r.json())
.then(raw => {
InteractionManager.runAfterInteractions(() => {
const processed = heavyParse(raw); // CPU‑intensive
setData(processed);
setLoading(false);
});
});
}, []);
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
{loading ? <ActivityIndicator /> : <Text>{data}</Text>}
</View>
);
}
function heavyParse(obj: any): string {
// Pretend this loops over thousands of items
let result = '';
for (let i = 0; i < 10000; i++) result += obj[i]?.value ?? '';
return result;
}
Takeaway
Wrap any CPU‑heavy logic in InteractionManager.runAfterInteractions to let animations and gestures finish first. This simple pattern often yields noticeable smoothness without architectural changes.