Skip to content

Commit 1200238

Browse files
authored
Add scroll performance example (#8364)
## Summary This PR adds scroll performance example which was based on the repro provided by @WadhahEssam in #8250. ## Test plan
1 parent 461f259 commit 1200238

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useEffect } from 'react';
2+
import { ScrollView, StyleSheet, View } from 'react-native';
3+
import Animated, {
4+
useAnimatedStyle,
5+
useSharedValue,
6+
withRepeat,
7+
withTiming,
8+
} from 'react-native-reanimated';
9+
10+
export default function ScrollPerformanceExample() {
11+
return (
12+
<ScrollView contentContainerStyle={styles.container}>
13+
{Array.from({ length: 500 }).map((_, index) => (
14+
<Item key={index} />
15+
))}
16+
</ScrollView>
17+
);
18+
}
19+
20+
function Item() {
21+
const sv = useSharedValue(0);
22+
23+
const animatedStyle = useAnimatedStyle(() => {
24+
return { opacity: sv.value };
25+
});
26+
27+
useEffect(() => {
28+
sv.value = withRepeat(
29+
withTiming(1, { duration: Math.random() * 1000 }),
30+
-1,
31+
true
32+
);
33+
}, [sv]);
34+
35+
return (
36+
<View style={styles.item}>
37+
<Animated.View
38+
style={[
39+
animatedStyle,
40+
styles.box,
41+
{ backgroundColor: Math.random() > 0.5 ? 'red' : 'blue' },
42+
]}
43+
/>
44+
</View>
45+
);
46+
}
47+
48+
const styles = StyleSheet.create({
49+
container: {
50+
gap: 10,
51+
},
52+
item: {
53+
flexDirection: 'row',
54+
width: '100%',
55+
justifyContent: 'center',
56+
alignItems: 'center',
57+
},
58+
box: {
59+
width: 100,
60+
height: 100,
61+
},
62+
});

apps/common-app/src/apps/reanimated/examples/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import ScreenStackHeaderConfigBackgroundColorExample from './ScreenStackHeaderCo
109109
import ScreenTransitionExample from './ScreenTransitionExample';
110110
import ScrollableViewExample from './ScrollableViewExample';
111111
import ScrollEventExample from './ScrollEventExample';
112+
import ScrollPerformanceExample from './ScrollPerformanceExample';
112113
import ScrollToExample from './ScrollToExample';
113114
import ScrollViewExample from './ScrollViewExample';
114115
import ScrollViewOffsetExample from './ScrollViewOffsetExample';
@@ -154,6 +155,11 @@ export const EXAMPLES: Record<string, Example> = {
154155
title: 'FPS',
155156
screen: FpsExample,
156157
},
158+
ScrollPerformanceExample: {
159+
icon: '🚁',
160+
title: 'Scroll performance',
161+
screen: ScrollPerformanceExample,
162+
},
157163
ThirdPartyComponentsExample: {
158164
icon: '3️⃣',
159165
title: 'Third party components',

0 commit comments

Comments
 (0)