Skip to content

Commit a61ce57

Browse files
PR Batch Testerclaude
andcommitted
refactor(mobile): memoize the shimmer interpolations
Building them in render made every re-render recreate and reconnect the native animation nodes. Also use useAnimatedValue and make the doc note say what to do. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 13a2dae commit a61ce57

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
Pressable,
4141
ScrollView,
4242
StyleSheet,
43+
useAnimatedValue,
4344
View,
4445
} from "react-native";
4546
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
@@ -264,7 +265,7 @@ export function ShimmeringWorkContent(props: {
264265
const [appIsActive, setAppIsActive] = useState(AppState.currentState === "active");
265266
const [reducedMotion, setReducedMotion] = useState(true);
266267
const screenIsFocused = useIsFocused();
267-
const [progress] = useState(() => new NativeAnimated.Value(0));
268+
const progress = useAnimatedValue(0);
268269
const gradientId = `work-shimmer-${useId().replaceAll(":", "")}`;
269270
const contentWidth = Math.min(
270271
availableWidth,
@@ -307,15 +308,20 @@ export function ShimmeringWorkContent(props: {
307308
}, [appIsActive, contentWidth, progress, reducedMotion, screenIsFocused]);
308309

309310
// One timing covers the sweep and the pause, so the native driver can loop it.
310-
const sweepInputRange = [0, SHIMMER_SWEEP_MS / (SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS), 1];
311-
const sweepTranslateX = progress.interpolate({
312-
inputRange: sweepInputRange,
313-
outputRange: [-SHIMMER_WIDTH, contentWidth, contentWidth],
314-
});
315-
const counterSweepTranslateX = progress.interpolate({
316-
inputRange: sweepInputRange,
317-
outputRange: [SHIMMER_WIDTH, -contentWidth, -contentWidth],
318-
});
311+
// Memoized so re-renders don't rebuild the native animation nodes.
312+
const { sweepTranslateX, counterSweepTranslateX } = useMemo(() => {
313+
const inputRange = [0, SHIMMER_SWEEP_MS / (SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS), 1];
314+
return {
315+
sweepTranslateX: progress.interpolate({
316+
inputRange,
317+
outputRange: [-SHIMMER_WIDTH, contentWidth, contentWidth],
318+
}),
319+
counterSweepTranslateX: progress.interpolate({
320+
inputRange,
321+
outputRange: [SHIMMER_WIDTH, -contentWidth, -contentWidth],
322+
}),
323+
};
324+
}, [contentWidth, progress]);
319325

320326
return (
321327
<View

‎docs/internals/performance-regressions.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Focused coverage: `ProjectionSettlement.test.ts`, `ThreadSettlementService.test.
109109
avoiding animates on iOS. Reanimated commits each animation frame to the shadow tree, and without
110110
commit pausing those commits can starve React's: the JS thread keeps retrying its commit, the
111111
screen freezes, and back does nothing. Release builds retry forever; dev builds abort with
112-
`ShadowTree.cpp: assertion failed (attempts < 1024)`. A looping animation that stays on screen
113-
while heavy content renders, such as the running-thread shimmer in `thread-work-log.tsx`, runs on
114-
React Native's `Animated` with `useNativeDriver: true`, which updates views without commits.
112+
`ShadowTree.cpp: assertion failed (attempts < 1024)`. Looping animations that stay on screen while
113+
heavy content renders should use React Native's `Animated` with `useNativeDriver: true`, which
114+
updates views without commits, not Reanimated's `withRepeat`. The running-thread shimmer in
115+
`thread-work-log.tsx` is the example.

0 commit comments

Comments
 (0)