@@ -23,12 +23,6 @@ import CustomTabBarExample from './CustomTabBarExample';
2323import CoverflowExample from './CoverflowExample' ;
2424import TabBarGapExample from './TabBarGapExample' ;
2525
26- type State = {
27- title : string ;
28- index : number ;
29- restoring : boolean ;
30- } ;
31-
3226type ExampleComponentType = React . ComponentType < { } > & {
3327 title : string ;
3428 tintColor ?: string ;
@@ -51,21 +45,14 @@ const EXAMPLE_COMPONENTS: ExampleComponentType[] = [
5145 TabBarGapExample ,
5246] ;
5347
54- const KeepAwake = ( ) => {
48+ const ExampleList = ( ) => {
5549 useKeepAwake ( ) ;
56- return null ;
57- } ;
50+ const [ index , setIndex ] = React . useState ( - 1 ) ;
51+ const [ restoring , setRestoring ] = React . useState ( false ) ;
5852
59- export default class ExampleList extends React . Component < any , State > {
60- state = {
61- title : 'Examples' ,
62- index : - 1 ,
63- restoring : false ,
64- } ;
65-
66- componentDidMount ( ) {
53+ React . useEffect ( ( ) => {
6754 if ( process . env . NODE_ENV !== 'production' ) {
68- this . restoreNavigationState ( ) ;
55+ restoreNavigationState ( ) ;
6956 }
7057
7158 [
@@ -78,18 +65,16 @@ export default class ExampleList extends React.Component<any, State> {
7865 require ( '../assets/album-art-7.jpg' ) ,
7966 require ( '../assets/album-art-8.jpg' ) ,
8067 ] . map ( ( image ) => Asset . fromModule ( image ) . downloadAsync ( ) ) ;
81- }
68+ } , [ ] ) ;
8269
83- private persistNavigationState = async ( currentIndex : number ) => {
70+ const persistNavigationState = async ( currentIndex : number ) => {
8471 if ( process . env . NODE_ENV !== 'production' ) {
8572 await AsyncStorage . setItem ( PERSISTENCE_KEY , JSON . stringify ( currentIndex ) ) ;
8673 }
8774 } ;
8875
89- private restoreNavigationState = async ( ) => {
90- this . setState ( {
91- restoring : true ,
92- } ) ;
76+ const restoreNavigationState = async ( ) => {
77+ setRestoring ( true ) ;
9378
9479 const savedIndexString = await AsyncStorage . getItem ( PERSISTENCE_KEY ) ;
9580
@@ -101,129 +86,114 @@ export default class ExampleList extends React.Component<any, State> {
10186 savedIndex >= 0 &&
10287 savedIndex < EXAMPLE_COMPONENTS . length
10388 ) {
104- this . setState ( {
105- index : savedIndex ,
106- } ) ;
89+ setIndex ( savedIndex ) ;
10790 }
10891 } catch ( e ) {
10992 // ignore
11093 }
11194
112- this . setState ( {
113- restoring : false ,
114- } ) ;
95+ setRestoring ( false ) ;
11596 } ;
11697
117- private handleNavigate = ( index : number ) => {
118- this . setState ( {
119- index,
120- } ) ;
121- this . persistNavigationState ( index ) ;
98+ const handleNavigate = ( index : number ) => {
99+ setIndex ( index ) ;
100+ persistNavigationState ( index ) ;
122101 } ;
123102
124- private handleNavigateBack = ( ) => {
125- this . handleNavigate ( - 1 ) ;
103+ const handleNavigateBack = ( ) => {
104+ handleNavigate ( - 1 ) ;
126105 } ;
127106
128- private renderItem = ( component : ExampleComponentType , i : number ) => (
107+ const renderItem = ( component : ExampleComponentType , i : number ) => (
129108 < TouchableOpacity
130109 key = { i }
131110 style = { styles . touchable }
132- onPress = { ( ) => this . handleNavigate ( i ) }
111+ onPress = { ( ) => handleNavigate ( i ) }
133112 >
134113 < Text style = { styles . item } >
135114 { i + 1 } . { component . title }
136115 </ Text >
137116 </ TouchableOpacity >
138117 ) ;
139118
140- render ( ) {
141- if ( this . state . restoring ) {
142- return null ;
143- }
144-
145- const { index } = this . state ;
146-
147- const ExampleComponent = EXAMPLE_COMPONENTS [ index ] || null ;
148- const backgroundColor = ExampleComponent ?. backgroundColor
149- ? ExampleComponent . backgroundColor
150- : '#111' ;
151- const tintColor =
152- ExampleComponent && typeof ExampleComponent . tintColor === 'string'
153- ? ExampleComponent . tintColor
154- : 'white' ;
155- const appbarElevation =
156- ExampleComponent && typeof ExampleComponent . appbarElevation === 'number'
157- ? ExampleComponent . appbarElevation
158- : 4 ;
159- const statusBarStyle =
160- ExampleComponent && typeof ExampleComponent . statusBarStyle === 'string'
161- ? ExampleComponent . statusBarStyle
162- : 'light-content' ;
163- const borderBottomWidth =
164- Platform . OS === 'ios' ? StyleSheet . hairlineWidth : 0 ;
119+ if ( restoring ) {
120+ return null ;
121+ }
165122
166- return (
167- < SafeAreaProvider >
168- < StatusBar
169- translucent
170- barStyle = { Platform . OS === 'ios' ? statusBarStyle : 'light-content' }
171- />
172- < KeepAwake />
173- < View style = { styles . container } >
174- < SafeAreaView
175- edges = { [ 'top' ] }
176- style = { [
177- styles . appbar ,
178- backgroundColor ? { backgroundColor } : null ,
179- appbarElevation
180- ? { elevation : appbarElevation , borderBottomWidth }
181- : null ,
182- ] }
183- >
184- < View style = { styles . appbarContent } >
185- { index > - 1 ? (
186- < TouchableOpacity
187- style = { styles . button }
188- onPress = { this . handleNavigateBack }
189- >
190- < Ionicons
191- name = {
192- Platform . OS === 'android'
193- ? 'md-arrow-back'
194- : 'ios-arrow-back'
195- }
196- size = { 24 }
197- color = { tintColor }
198- />
199- </ TouchableOpacity >
200- ) : null }
201- < Text
202- style = { [ styles . title , tintColor ? { color : tintColor } : null ] }
123+ const ExampleComponent = EXAMPLE_COMPONENTS [ index ] || null ;
124+ const backgroundColor = ExampleComponent ?. backgroundColor
125+ ? ExampleComponent . backgroundColor
126+ : '#111' ;
127+ const tintColor =
128+ ExampleComponent && typeof ExampleComponent . tintColor === 'string'
129+ ? ExampleComponent . tintColor
130+ : 'white' ;
131+ const appbarElevation =
132+ ExampleComponent && typeof ExampleComponent . appbarElevation === 'number'
133+ ? ExampleComponent . appbarElevation
134+ : 4 ;
135+ const statusBarStyle =
136+ ExampleComponent && typeof ExampleComponent . statusBarStyle === 'string'
137+ ? ExampleComponent . statusBarStyle
138+ : 'light-content' ;
139+ const borderBottomWidth =
140+ Platform . OS === 'ios' ? StyleSheet . hairlineWidth : 0 ;
141+
142+ return (
143+ < SafeAreaProvider >
144+ < StatusBar
145+ translucent
146+ barStyle = { Platform . OS === 'ios' ? statusBarStyle : 'light-content' }
147+ />
148+ < View style = { styles . container } >
149+ < SafeAreaView
150+ edges = { [ 'top' ] }
151+ style = { [
152+ styles . appbar ,
153+ backgroundColor ? { backgroundColor } : null ,
154+ appbarElevation
155+ ? { elevation : appbarElevation , borderBottomWidth }
156+ : null ,
157+ ] }
158+ >
159+ < View style = { styles . appbarContent } >
160+ { index > - 1 ? (
161+ < TouchableOpacity
162+ style = { styles . button }
163+ onPress = { handleNavigateBack }
203164 >
204- { index > - 1
205- ? EXAMPLE_COMPONENTS [ index ] . title
206- : this . state . title }
207- </ Text >
208- { index > - 1 ? < View style = { styles . button } /> : null }
209- </ View >
210- </ SafeAreaView >
211- < SafeAreaView edges = { [ 'bottom' ] } style = { styles . safearea } >
212- < View style = { styles . content } >
213- { index === - 1 ? (
214- < ScrollView >
215- { EXAMPLE_COMPONENTS . map ( this . renderItem ) }
216- </ ScrollView >
217- ) : ExampleComponent ? (
218- < ExampleComponent />
219- ) : null }
220- </ View >
221- </ SafeAreaView >
222- </ View >
223- </ SafeAreaProvider >
224- ) ;
225- }
226- }
165+ < Ionicons
166+ name = {
167+ Platform . OS === 'android'
168+ ? 'md-arrow-back'
169+ : 'ios-arrow-back'
170+ }
171+ size = { 24 }
172+ color = { tintColor }
173+ />
174+ </ TouchableOpacity >
175+ ) : null }
176+ < Text
177+ style = { [ styles . title , tintColor ? { color : tintColor } : null ] }
178+ >
179+ { index > - 1 ? EXAMPLE_COMPONENTS [ index ] . title : 'Examples' }
180+ </ Text >
181+ { index > - 1 ? < View style = { styles . button } /> : null }
182+ </ View >
183+ </ SafeAreaView >
184+ < SafeAreaView edges = { [ 'bottom' ] } style = { styles . safearea } >
185+ < View style = { styles . content } >
186+ { index === - 1 ? (
187+ < ScrollView > { EXAMPLE_COMPONENTS . map ( renderItem ) } </ ScrollView >
188+ ) : ExampleComponent ? (
189+ < ExampleComponent />
190+ ) : null }
191+ </ View >
192+ </ SafeAreaView >
193+ </ View >
194+ </ SafeAreaProvider >
195+ ) ;
196+ } ;
227197
228198const styles = StyleSheet . create ( {
229199 container : {
0 commit comments