-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExample.js
64 lines (57 loc) · 1.6 KB
/
Example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import { View, TouchableOpacity, Text } from 'react-native';
const getAvailableRoutes = navigation => {
let availableRoutes = [];
if (!navigation) return availableRoutes;
const parent = navigation.dangerouslyGetParent();
if (parent) {
if (parent.router && parent.router.childRouters) {
// Grab all the routes the parent defines and add it the list
availableRoutes = [
...availableRoutes,
...Object.keys(parent.router.childRouters),
];
}
// Recursively work up the tree until there are none left
availableRoutes = [...availableRoutes, ...getAvailableRoutes(parent)];
}
// De-dupe the list and then remove the current route from the list
return [...new Set(availableRoutes)].filter(
route => route !== navigation.state.routeName
);
};
const getRandomColor = () => {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
const Example = ({ navigation }) => {
return (
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: getRandomColor(),
}}
>
{getAvailableRoutes(navigation).map(route => (
<TouchableOpacity
onPress={() => navigation.navigate(route)}
key={route}
style={{
backgroundColor: '#fff',
padding: 10,
margin: 10,
}}
>
<Text>{route}</Text>
</TouchableOpacity>
))}
</View>
);
};
export default Example;