@@ -20,7 +20,6 @@ const SnapshotViewIOS = require('./examples/Snapshot/SnapshotViewIOS.ios');
20
20
const URIActionMap = require ( './utils/URIActionMap' ) ;
21
21
22
22
const {
23
- Appearance,
24
23
AppRegistry,
25
24
AsyncStorage,
26
25
BackHandler,
@@ -30,6 +29,7 @@ const {
30
29
SafeAreaView,
31
30
StyleSheet,
32
31
Text,
32
+ useColorScheme,
33
33
View,
34
34
YellowBox,
35
35
} = require ( 'react-native' ) ;
@@ -38,6 +38,7 @@ import type {RNTesterExample} from './types/RNTesterTypes';
38
38
import type { RNTesterAction } from './utils/RNTesterActions' ;
39
39
import type { RNTesterNavigationState } from './utils/RNTesterNavigationReducer' ;
40
40
import { RNTesterThemeContext , themes } from './components/RNTesterTheme' ;
41
+ import type { ColorSchemeName } from '../../Libraries/Utilities/NativeAppearance' ;
41
42
42
43
type Props = {
43
44
exampleFromAppetizeParams ?: ?string ,
@@ -86,6 +87,49 @@ const Header = ({onBack, title}: {onBack?: () => mixed, title: string}) => (
86
87
</ RNTesterThemeContext . Consumer >
87
88
) ;
88
89
90
+ const RNTesterExampleContainerViaHook = ( {
91
+ onBack,
92
+ title,
93
+ module,
94
+ } : {
95
+ onBack ?: ( ) => mixed ,
96
+ title : string ,
97
+ module : RNTesterExample ,
98
+ } ) => {
99
+ const colorScheme : ?ColorSchemeName = useColorScheme ( ) ;
100
+ const theme = colorScheme === 'dark' ? themes . dark : themes . light ;
101
+ return (
102
+ < RNTesterThemeContext . Provider value = { theme } >
103
+ < View style = { styles . exampleContainer } >
104
+ < Header onBack = { onBack } title = { title } />
105
+ < RNTesterExampleContainer module = { module } />
106
+ </ View >
107
+ </ RNTesterThemeContext . Provider >
108
+ ) ;
109
+ } ;
110
+
111
+ const RNTesterExampleListViaHook = ( {
112
+ onNavigate,
113
+ list,
114
+ } : {
115
+ onNavigate ? : ( ) => mixed ,
116
+ list : {
117
+ ComponentExamples : Array < RNTesterExample > ,
118
+ APIExamples : Array < RNTesterExample > ,
119
+ } ,
120
+ } ) = > {
121
+ const colorScheme : ?ColorSchemeName = useColorScheme ( ) ;
122
+ const theme = colorScheme === 'dark' ? themes . dark : themes . light ;
123
+ return (
124
+ < RNTesterThemeContext . Provider value = { theme } >
125
+ < View style = { styles . exampleContainer } >
126
+ < Header title = "RNTester" />
127
+ < RNTesterExampleList onNavigate = { onNavigate } list = { list } />
128
+ </ View >
129
+ </ RNTesterThemeContext . Provider >
130
+ ) ;
131
+ } ;
132
+
89
133
class RNTesterApp extends React . Component < Props , RNTesterNavigationState > {
90
134
_mounted : boolean ;
91
135
@@ -113,14 +157,6 @@ class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
113
157
Linking . addEventListener ( 'url' , url => {
114
158
this . _handleAction ( URIActionMap ( url ) ) ;
115
159
} ) ;
116
-
117
- Appearance . addChangeListener ( prefs => {
118
- this . _handleAction (
119
- RNTesterActions . ThemeAction (
120
- prefs . colorScheme === 'dark' ? themes . dark : themes . light ,
121
- ) ,
122
- ) ;
123
- } ) ;
124
160
}
125
161
126
162
componentWillUnmount ( ) {
@@ -147,32 +183,25 @@ class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
147
183
if ( ! this . state ) {
148
184
return null ;
149
185
}
150
- const theme = this . state . theme ;
151
186
if ( this . state . openExample ) {
152
187
const Component = RNTesterList . Modules [ this . state . openExample ] ;
153
188
if ( Component && Component . external ) {
154
189
return < Component onExampleExit = { this . _handleBack } /> ;
155
190
} else {
156
191
return (
157
- < RNTesterThemeContext . Provider value = { theme } >
158
- < View style = { styles . exampleContainer } >
159
- < Header onBack = { this . _handleBack } title = { Component . title } />
160
- < RNTesterExampleContainer module = { Component } />
161
- </ View >
162
- </ RNTesterThemeContext . Provider >
192
+ < RNTesterExampleContainerViaHook
193
+ onBack = { this . _handleBack }
194
+ title = { Component . title }
195
+ module = { Component }
196
+ />
163
197
) ;
164
198
}
165
199
}
166
200
return (
167
- < RNTesterThemeContext . Provider value = { theme } >
168
- < View style = { styles . exampleContainer } >
169
- < Header title = "RNTester" />
170
- < RNTesterExampleList
171
- onNavigate = { this . _handleAction }
172
- list = { RNTesterList }
173
- />
174
- </ View >
175
- </ RNTesterThemeContext . Provider >
201
+ < RNTesterExampleListViaHook
202
+ onNavigate = { this . _handleAction }
203
+ list = { RNTesterList }
204
+ />
176
205
) ;
177
206
}
178
207
}
0 commit comments