|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Button, StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { |
| 4 | + StackContainer, |
| 5 | + useStackNavigationContext, |
| 6 | +} from '@apps/shared/containers/stack'; |
| 7 | +import { CenteredLayoutView } from '@apps/shared/CenteredLayoutView'; |
| 8 | + |
| 9 | +const ROUTE_NAMES = [ |
| 10 | + 'SharedOne', |
| 11 | + 'SharedTwo', |
| 12 | + 'InlineOne', |
| 13 | + 'InlineTwo', |
| 14 | +] as const; |
| 15 | + |
| 16 | +function StatefulScreen({ |
| 17 | + title, |
| 18 | + backgroundColor, |
| 19 | +}: { |
| 20 | + title: string; |
| 21 | + backgroundColor: string; |
| 22 | +}) { |
| 23 | + const [count, setCount] = useState(0); |
| 24 | + const navigation = useStackNavigationContext(); |
| 25 | + |
| 26 | + return ( |
| 27 | + <CenteredLayoutView style={{ backgroundColor }}> |
| 28 | + <Text style={styles.title}>{title}</Text> |
| 29 | + <Text style={styles.meta}>routeKey: {navigation.routeKey}</Text> |
| 30 | + <Text style={styles.counter}>Local count: {count}</Text> |
| 31 | + |
| 32 | + <Button title="+ Local state" onPress={() => setCount(c => c + 1)} /> |
| 33 | + |
| 34 | + <View style={styles.row}> |
| 35 | + {ROUTE_NAMES.map(name => ( |
| 36 | + <Button |
| 37 | + key={name} |
| 38 | + title={`Push ${name}`} |
| 39 | + onPress={() => navigation.push(name)} |
| 40 | + /> |
| 41 | + ))} |
| 42 | + </View> |
| 43 | + |
| 44 | + <Button title="Pop" onPress={() => navigation.pop(navigation.routeKey)} /> |
| 45 | + </CenteredLayoutView> |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +// Same element object reused by SharedOne + SharedTwo — exercises StackContainer |
| 50 | +// rendering without cloneElement (state/identity sharing across stack entries). |
| 51 | +const SharedRef = ( |
| 52 | + <StatefulScreen title="Shared Ref (same object)" backgroundColor="#E6E6FA" /> |
| 53 | +); |
| 54 | + |
| 55 | +export default function TestStackPoC() { |
| 56 | + return ( |
| 57 | + <StackContainer |
| 58 | + routeConfigs={[ |
| 59 | + { |
| 60 | + name: 'SharedOne', |
| 61 | + element: SharedRef, |
| 62 | + options: {}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'SharedTwo', |
| 66 | + element: SharedRef, |
| 67 | + options: {}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'InlineOne', |
| 71 | + element: ( |
| 72 | + <StatefulScreen title="Inline One" backgroundColor="#FFE4C4" /> |
| 73 | + ), |
| 74 | + options: {}, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: 'InlineTwo', |
| 78 | + element: ( |
| 79 | + <StatefulScreen title="Inline Two" backgroundColor="#FFE4C4" /> |
| 80 | + ), |
| 81 | + options: {}, |
| 82 | + }, |
| 83 | + ]} |
| 84 | + /> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +const styles = StyleSheet.create({ |
| 89 | + title: { fontSize: 20, fontWeight: 'bold' }, |
| 90 | + meta: { fontSize: 14, marginVertical: 4 }, |
| 91 | + counter: { fontSize: 24, fontWeight: '600', marginVertical: 8 }, |
| 92 | + row: { gap: 4, alignItems: 'stretch', width: '100%', paddingHorizontal: 16 }, |
| 93 | +}); |
0 commit comments