Skip to content

Commit 2d1435e

Browse files
authored
Shadow Nodes Cloning example for the example app (#8505)
## Summary An example for the example app with goal of testing performance of the shadow nodes cloning process. ## Test plan --------- Signed-off-by: Oskar Pawica <[email protected]>
1 parent 9347064 commit 2d1435e

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import React, { useEffect, useMemo, useState } from 'react';
2+
import { StyleSheet, Text, View } from 'react-native';
3+
import Animated, {
4+
useSharedValue,
5+
withRepeat,
6+
withTiming,
7+
} from 'react-native-reanimated';
8+
9+
function AnimatedView() {
10+
const opacitySv = useSharedValue(0);
11+
const scaleSv = useSharedValue(0);
12+
const [counter, setCounter] = useState(0);
13+
14+
useEffect(() => {
15+
opacitySv.value = withRepeat(
16+
withTiming(Math.random(), { duration: 600 }),
17+
-1,
18+
true
19+
);
20+
scaleSv.value = withRepeat(
21+
withTiming(Math.random() * 3, { duration: 600 }),
22+
-1,
23+
true
24+
);
25+
26+
const interval = setInterval(() => {
27+
setCounter((prevCounter) => prevCounter + 1);
28+
}, 1000);
29+
30+
return () => clearInterval(interval);
31+
}, [opacitySv, scaleSv, setCounter]);
32+
33+
const style = useMemo(
34+
() => [
35+
styles.animatedView,
36+
{
37+
opacity: opacitySv,
38+
transform: [{ scale: scaleSv }],
39+
},
40+
],
41+
[opacitySv, scaleSv]
42+
);
43+
44+
return (
45+
<Animated.View collapsable={false} style={style}>
46+
<Text>{counter}</Text>
47+
</Animated.View>
48+
);
49+
}
50+
51+
function RecursiveView(props: { depth: number; extraChildrenCount: number }) {
52+
const children = useMemo(
53+
() =>
54+
Array.from({ length: props.extraChildrenCount }, (_, idx) => (
55+
<View collapsable={false} key={idx} />
56+
)),
57+
[props.extraChildrenCount]
58+
);
59+
60+
if (props.depth === 0) {
61+
return <AnimatedView />;
62+
}
63+
64+
return (
65+
<View collapsable={false}>
66+
<RecursiveView
67+
depth={props.depth - 1}
68+
extraChildrenCount={props.extraChildrenCount}
69+
/>
70+
{children}
71+
</View>
72+
);
73+
}
74+
75+
export default function ShadowNodesCloningExample() {
76+
return (
77+
<View style={styles.container}>
78+
<RecursiveView depth={100} extraChildrenCount={100} />
79+
</View>
80+
);
81+
}
82+
83+
const styles = StyleSheet.create({
84+
container: {
85+
flex: 1,
86+
alignItems: 'center',
87+
justifyContent: 'center',
88+
},
89+
animatedView: {
90+
width: 32,
91+
height: 32,
92+
backgroundColor: 'red',
93+
},
94+
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ import WorkletExample from './WorkletExample';
137137
import WorkletFactoryCrash from './WorkletFactoryCrashExample';
138138
import WorkletRuntimeExample from './WorkletRuntimeExample';
139139
import InstanceDiscoveryExample from './InstanceDiscoveryExample';
140+
import ShadowNodesCloningExample from './ShadowNodesCloningExample';
140141

141142
export const REAPlatform = {
142143
IOS: 'ios',
@@ -624,6 +625,11 @@ export const EXAMPLES: Record<string, Example> = {
624625
screen: PlatformColorExample,
625626
icon: '🎨',
626627
},
628+
ShadowNodesCloningExample: {
629+
icon: '🌑',
630+
title: 'Shadow Nodes Cloning',
631+
screen: ShadowNodesCloningExample,
632+
},
627633

628634
// Old examples
629635
AnimatedStyleUpdateExample: {

0 commit comments

Comments
 (0)