|
| 1 | +import React from 'react' |
| 2 | +import { ScrollView, Text, View } from 'react-native' |
| 3 | +import { createStyleSheet, useStyles, UnistylesProvider, UnistylesRuntime } from 'react-native-unistyles' |
| 4 | +import { Button, DemoScreen } from '../components' |
| 5 | + |
| 6 | +type BoxProps = { |
| 7 | + index: number |
| 8 | +} |
| 9 | + |
| 10 | +export const ReactProviderScreen: React.FunctionComponent = () => ( |
| 11 | + <UnistylesProvider> |
| 12 | + <ReactProviderScreenScreenContent /> |
| 13 | + </UnistylesProvider> |
| 14 | +) |
| 15 | + |
| 16 | +const Box: React.FunctionComponent<BoxProps> = ({ index }) => { |
| 17 | + const { styles } = useStyles(stylesheet) |
| 18 | + |
| 19 | + return ( |
| 20 | + <View style={styles.box}> |
| 21 | + <Text style={styles.text}> |
| 22 | + {index + 1} |
| 23 | + </Text> |
| 24 | + </View> |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +const ReactProviderScreenScreenContent: React.FunctionComponent = () => { |
| 29 | + const { styles } = useStyles(stylesheet) |
| 30 | + |
| 31 | + return ( |
| 32 | + <DemoScreen> |
| 33 | + <View style={styles.container}> |
| 34 | + <Text style={styles.title}> |
| 35 | + This screen uses UnistylesProvider that may help if your app have hundreds of useStyles hooks. |
| 36 | + </Text> |
| 37 | + <Text style={styles.title}> |
| 38 | + Rendering 1000 boxes with 1000 useStyles hooks: |
| 39 | + </Text> |
| 40 | + <ScrollView showsVerticalScrollIndicator={false}> |
| 41 | + {Array.from({ length: 1000 }).map((_, index) => ( |
| 42 | + <Box |
| 43 | + key={index} |
| 44 | + index={index} |
| 45 | + /> |
| 46 | + ))} |
| 47 | + </ScrollView> |
| 48 | + </View> |
| 49 | + <View style={styles.absolutView}> |
| 50 | + <Button |
| 51 | + alignCenter |
| 52 | + title="Change theme" |
| 53 | + onPress={() => { |
| 54 | + UnistylesRuntime.themeName === 'light' |
| 55 | + ? UnistylesRuntime.setTheme('dark') |
| 56 | + : UnistylesRuntime.setTheme('light') |
| 57 | + }} |
| 58 | + /> |
| 59 | + </View> |
| 60 | + </DemoScreen> |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +const stylesheet = createStyleSheet(theme => ({ |
| 65 | + container: { |
| 66 | + flex: 1, |
| 67 | + justifyContent: 'center', |
| 68 | + alignItems: 'center', |
| 69 | + paddingHorizontal: 20, |
| 70 | + backgroundColor: theme.colors.backgroundColor, |
| 71 | + rowGap: 20 |
| 72 | + }, |
| 73 | + title: { |
| 74 | + fontSize: 20, |
| 75 | + textAlign: 'center', |
| 76 | + color: theme.colors.typography |
| 77 | + }, |
| 78 | + text: { |
| 79 | + textAlign: 'center', |
| 80 | + color: theme.colors.typography |
| 81 | + }, |
| 82 | + boxes: { |
| 83 | + flexDirection: 'row' |
| 84 | + }, |
| 85 | + box: { |
| 86 | + height: 40, |
| 87 | + width: 40, |
| 88 | + justifyContent: 'center', |
| 89 | + marginBottom: 10, |
| 90 | + backgroundColor: theme.colors.accent |
| 91 | + }, |
| 92 | + absolutView: { |
| 93 | + position: 'absolute', |
| 94 | + bottom: 0, |
| 95 | + left: 50, |
| 96 | + right: 50, |
| 97 | + height: 100 |
| 98 | + } |
| 99 | +})) |
0 commit comments