~/swaraj.dev
Back to all posts
April 27, 20261 min read

Deferring Heavy Work with InteractionManager

Learn how to keep animations smooth by postponing expensive calculations until after interactions finish, using React Native's InteractionManager.

react nativeperformanceui

Insight

When a screen transition or animation starts, any synchronous JavaScript work can block the UI thread, causing janky frames. React Native's InteractionManager lets you schedule heavy tasks—like data parsing or image processing—to run after all ongoing interactions settle. This pattern is especially useful for splash screens, list pagination, or complex layout calculations that aren't needed immediately.

Example

import { InteractionManager } from 'react-native';
import { useEffect, useState } from 'react';

export default function HeavyScreen() {
  const [data, setData] = useState<string[]>([]);

  useEffect(() => {
    const task = InteractionManager.runAfterInteractions(() => {
      // Simulate expensive parsing
      const parsed = Array.from({ length: 5000 }, (_, i) => `Item ${i}`);
      setData(parsed);
    });
    return () => task.cancel();
  }, []);

  return <FlatList data={data} renderItem={({ item }) => <Text>{item}</Text>} />;
}

Takeaway

Wrap non‑essential, CPU‑intensive work in InteractionManager.runAfterInteractions to preserve frame rates during critical UI interactions.