Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.

Commit 69cbb62

Browse files
authored
chore: update example to funcitonal components (#1389)
1 parent 77dec4f commit 69cbb62

File tree

7 files changed

+296
-364
lines changed

7 files changed

+296
-364
lines changed

example/src/App.tsx

Lines changed: 94 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ import CustomTabBarExample from './CustomTabBarExample';
2323
import CoverflowExample from './CoverflowExample';
2424
import TabBarGapExample from './TabBarGapExample';
2525

26-
type State = {
27-
title: string;
28-
index: number;
29-
restoring: boolean;
30-
};
31-
3226
type 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

228198
const styles = StyleSheet.create({
229199
container: {

example/src/AutoWidthTabBarExample.tsx

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,18 @@ type State = NavigationState<{
1717
title: string;
1818
}>;
1919

20-
export default class DynamicWidthTabBarExample extends React.Component<
21-
{},
22-
State
23-
> {
24-
// eslint-disable-next-line react/sort-comp
25-
static title = 'Scrollable tab bar (auto width)';
26-
static backgroundColor = '#3f51b5';
27-
static appbarElevation = 0;
20+
const AutoWidthTabBarExample = () => {
21+
const [index, onIndexChange] = React.useState(1);
22+
const [routes] = React.useState([
23+
{ key: 'article', title: 'Article' },
24+
{ key: 'contacts', title: 'Contacts' },
25+
{ key: 'albums', title: 'Albums' },
26+
{ key: 'chat', title: 'Chat' },
27+
{ key: 'long', title: 'long long long title' },
28+
{ key: 'medium', title: 'medium title' },
29+
]);
2830

29-
state = {
30-
index: 1,
31-
routes: [
32-
{ key: 'article', title: 'Article' },
33-
{ key: 'contacts', title: 'Contacts' },
34-
{ key: 'albums', title: 'Albums' },
35-
{ key: 'chat', title: 'Chat' },
36-
{ key: 'long', title: 'long long long title' },
37-
{ key: 'medium', title: 'medium title' },
38-
],
39-
};
40-
41-
private handleIndexChange = (index: number) =>
42-
this.setState({
43-
index,
44-
});
45-
46-
private renderTabBar = (
31+
const renderTabBar = (
4732
props: SceneRendererProps & { navigationState: State }
4833
) => (
4934
<TabBar
@@ -56,7 +41,7 @@ export default class DynamicWidthTabBarExample extends React.Component<
5641
/>
5742
);
5843

59-
private renderScene = SceneMap({
44+
const renderScene = SceneMap({
6045
albums: Albums,
6146
contacts: Contacts,
6247
article: Article,
@@ -65,17 +50,24 @@ export default class DynamicWidthTabBarExample extends React.Component<
6550
medium: Article,
6651
});
6752

68-
render() {
69-
return (
70-
<TabView
71-
navigationState={this.state}
72-
renderScene={this.renderScene}
73-
renderTabBar={this.renderTabBar}
74-
onIndexChange={this.handleIndexChange}
75-
/>
76-
);
77-
}
78-
}
53+
return (
54+
<TabView
55+
navigationState={{
56+
index,
57+
routes,
58+
}}
59+
renderScene={renderScene}
60+
renderTabBar={renderTabBar}
61+
onIndexChange={onIndexChange}
62+
/>
63+
);
64+
};
65+
66+
AutoWidthTabBarExample.title = 'Scrollable tab bar (auto width)';
67+
AutoWidthTabBarExample.backgroundColor = '#3f51b5';
68+
AutoWidthTabBarExample.appbarElevation = 0;
69+
70+
export default AutoWidthTabBarExample;
7971

8072
const styles = StyleSheet.create({
8173
tabbar: {

0 commit comments

Comments
 (0)