|
| 1 | +import * as React from 'react'; |
| 2 | +import { Platform, StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation'; |
| 4 | + |
| 5 | +const store = new Set<Dispatch>(); |
| 6 | + |
| 7 | +type Dispatch = (value: number) => void; |
| 8 | + |
| 9 | +function useValue() { |
| 10 | + const [value, setValue] = React.useState<number>(0); |
| 11 | + |
| 12 | + React.useEffect(() => { |
| 13 | + const dispatch = (value: number) => { |
| 14 | + setValue(value); |
| 15 | + }; |
| 16 | + store.add(dispatch); |
| 17 | + return () => { |
| 18 | + store.delete(dispatch); |
| 19 | + }; |
| 20 | + }, [setValue]); |
| 21 | + |
| 22 | + return value; |
| 23 | +} |
| 24 | + |
| 25 | +function HomeScreen() { |
| 26 | + return ( |
| 27 | + <View style={styles.screenContainer}> |
| 28 | + <Text>Home!</Text> |
| 29 | + </View> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +function DetailsScreen(props: any) { |
| 34 | + const value = useValue(); |
| 35 | + const screenName = props?.route?.params?.screenName; |
| 36 | + // only 1 'render' should appear at the time |
| 37 | + console.log(`${Platform.OS} Details Screen render ${value} ${screenName}`); |
| 38 | + return ( |
| 39 | + <View style={styles.screenContainer}> |
| 40 | + <Text>Details!</Text> |
| 41 | + <Text style={{ alignSelf: 'center' }}> |
| 42 | + Details Screen {value} {screenName ? screenName : ''}{' '} |
| 43 | + </Text> |
| 44 | + </View> |
| 45 | + ); |
| 46 | +} |
| 47 | +const Tab = createNativeBottomTabNavigator(); |
| 48 | + |
| 49 | +export default function NativeBottomTabsFreezeOnBlur() { |
| 50 | + React.useEffect(() => { |
| 51 | + let timer = 0; |
| 52 | + const interval = setInterval(() => { |
| 53 | + timer = timer + 1; |
| 54 | + store.forEach((dispatch) => dispatch(timer)); |
| 55 | + }, 3000); |
| 56 | + return () => clearInterval(interval); |
| 57 | + }, []); |
| 58 | + |
| 59 | + return ( |
| 60 | + <Tab.Navigator |
| 61 | + screenOptions={{ |
| 62 | + freezeOnBlur: true, |
| 63 | + }} |
| 64 | + > |
| 65 | + <Tab.Screen |
| 66 | + name="Article" |
| 67 | + component={HomeScreen} |
| 68 | + initialParams={{ |
| 69 | + screenName: 'Article', |
| 70 | + }} |
| 71 | + options={{ |
| 72 | + tabBarIcon: () => require('../../assets/icons/article_dark.png'), |
| 73 | + }} |
| 74 | + /> |
| 75 | + <Tab.Screen |
| 76 | + name="Albums" |
| 77 | + component={DetailsScreen} |
| 78 | + initialParams={{ |
| 79 | + screenName: 'Albums', |
| 80 | + }} |
| 81 | + options={{ |
| 82 | + tabBarIcon: () => require('../../assets/icons/grid_dark.png'), |
| 83 | + }} |
| 84 | + /> |
| 85 | + <Tab.Screen |
| 86 | + name="Contact" |
| 87 | + component={DetailsScreen} |
| 88 | + initialParams={{ |
| 89 | + screenName: 'Contact', |
| 90 | + }} |
| 91 | + options={{ |
| 92 | + tabBarIcon: () => require('../../assets/icons/person_dark.png'), |
| 93 | + }} |
| 94 | + /> |
| 95 | + <Tab.Screen |
| 96 | + name="Chat" |
| 97 | + component={DetailsScreen} |
| 98 | + initialParams={{ |
| 99 | + screenName: 'Chat', |
| 100 | + }} |
| 101 | + options={{ |
| 102 | + tabBarIcon: () => require('../../assets/icons/chat_dark.png'), |
| 103 | + }} |
| 104 | + /> |
| 105 | + </Tab.Navigator> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +const styles = StyleSheet.create({ |
| 110 | + screenContainer: { |
| 111 | + flex: 1, |
| 112 | + justifyContent: 'center', |
| 113 | + alignItems: 'center', |
| 114 | + }, |
| 115 | +}); |
0 commit comments