@@ -23,12 +23,6 @@ import CustomTabBarExample from './CustomTabBarExample';
23
23
import CoverflowExample from './CoverflowExample' ;
24
24
import TabBarGapExample from './TabBarGapExample' ;
25
25
26
- type State = {
27
- title : string ;
28
- index : number ;
29
- restoring : boolean ;
30
- } ;
31
-
32
26
type ExampleComponentType = React . ComponentType < { } > & {
33
27
title : string ;
34
28
tintColor ?: string ;
@@ -51,21 +45,14 @@ const EXAMPLE_COMPONENTS: ExampleComponentType[] = [
51
45
TabBarGapExample ,
52
46
] ;
53
47
54
- const KeepAwake = ( ) => {
48
+ const ExampleList = ( ) => {
55
49
useKeepAwake ( ) ;
56
- return null ;
57
- } ;
50
+ const [ index , setIndex ] = React . useState ( - 1 ) ;
51
+ const [ restoring , setRestoring ] = React . useState ( false ) ;
58
52
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 ( ( ) => {
67
54
if ( process . env . NODE_ENV !== 'production' ) {
68
- this . restoreNavigationState ( ) ;
55
+ restoreNavigationState ( ) ;
69
56
}
70
57
71
58
[
@@ -78,18 +65,16 @@ export default class ExampleList extends React.Component<any, State> {
78
65
require ( '../assets/album-art-7.jpg' ) ,
79
66
require ( '../assets/album-art-8.jpg' ) ,
80
67
] . map ( ( image ) => Asset . fromModule ( image ) . downloadAsync ( ) ) ;
81
- }
68
+ } , [ ] ) ;
82
69
83
- private persistNavigationState = async ( currentIndex : number ) => {
70
+ const persistNavigationState = async ( currentIndex : number ) => {
84
71
if ( process . env . NODE_ENV !== 'production' ) {
85
72
await AsyncStorage . setItem ( PERSISTENCE_KEY , JSON . stringify ( currentIndex ) ) ;
86
73
}
87
74
} ;
88
75
89
- private restoreNavigationState = async ( ) => {
90
- this . setState ( {
91
- restoring : true ,
92
- } ) ;
76
+ const restoreNavigationState = async ( ) => {
77
+ setRestoring ( true ) ;
93
78
94
79
const savedIndexString = await AsyncStorage . getItem ( PERSISTENCE_KEY ) ;
95
80
@@ -101,129 +86,114 @@ export default class ExampleList extends React.Component<any, State> {
101
86
savedIndex >= 0 &&
102
87
savedIndex < EXAMPLE_COMPONENTS . length
103
88
) {
104
- this . setState ( {
105
- index : savedIndex ,
106
- } ) ;
89
+ setIndex ( savedIndex ) ;
107
90
}
108
91
} catch ( e ) {
109
92
// ignore
110
93
}
111
94
112
- this . setState ( {
113
- restoring : false ,
114
- } ) ;
95
+ setRestoring ( false ) ;
115
96
} ;
116
97
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 ) ;
122
101
} ;
123
102
124
- private handleNavigateBack = ( ) => {
125
- this . handleNavigate ( - 1 ) ;
103
+ const handleNavigateBack = ( ) => {
104
+ handleNavigate ( - 1 ) ;
126
105
} ;
127
106
128
- private renderItem = ( component : ExampleComponentType , i : number ) => (
107
+ const renderItem = ( component : ExampleComponentType , i : number ) => (
129
108
< TouchableOpacity
130
109
key = { i }
131
110
style = { styles . touchable }
132
- onPress = { ( ) => this . handleNavigate ( i ) }
111
+ onPress = { ( ) => handleNavigate ( i ) }
133
112
>
134
113
< Text style = { styles . item } >
135
114
{ i + 1 } . { component . title }
136
115
</ Text >
137
116
</ TouchableOpacity >
138
117
) ;
139
118
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
+ }
165
122
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 }
203
164
>
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
+ } ;
227
197
228
198
const styles = StyleSheet . create ( {
229
199
container : {
0 commit comments