-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
91 lines (80 loc) · 2.51 KB
/
App.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { View, Text, FlatList, TouchableOpacity } from 'react-native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createContext, useContext, useEffect, useState } from 'react';
import { fetchAllPokemon, fetchPokemonDetailsByName } from './api';
const PokemonContext = createContext({
search: '',
setSearch: () => {},
pokemonIndex: [],
setPokemonIndex: () => {},
});
function PokemonIndex() {
const context = useContext(PokemonContext);
const { setPokemonIndex: setPokemonIndexContext } = context
const [pokemonIndex, setPokemonIndex] = useState([]);
useEffect(() => {
fetchAllPokemon().then(({ results }) => {
setPokemonIndex(results);
setPokemonIndexContext(results);
});
}, [setPokemonIndexContext]);
return (
<FlatList
style={{ flex: 1 }}
contentInsetAdjustmentBehavior={'automatic'}
keyExtractor={(pokemon) => pokemon.name}
data={pokemonIndex.filter((p) => !p.name.includes(context.search.toLowerCase()))}
renderItem={({ item: pokemon }) => <PokemonListItem pokemon={pokemon} />}
/>
);
}
const PokemonListItem = ({ pokemon }) => {
const { navigate } = useNavigation();
return (
<TouchableOpacity onPress={() => navigate('PokemonDetails', { pokemon })}>
<View style={{ flex: 1 }}>
<Text>{pokemon.name}</Text>
</View>
</TouchableOpacity>
);
};
function PokemonDetails({ route, navigation }) {
return (
<View style={{ flex: 1 }}>
<Text>Pokemon details here</Text>
</View>
);
}
const Stack = createNativeStackNavigator();
function App() {
const [pokemonIndex, setPokemonIndex] = useState([]);
const [search, setSearch] = useState('');
return (
<NavigationContainer>
<PokemonContext.Provider value={{ pokemonIndex, setPokemonIndex, search, setSearch }}>
<Stack.Navigator>
<Stack.Screen
name="Pokedex"
options={{
headerSearchBarOptions: {
onChangeText: (e) => {
setSearch(e.nativeEvent.text);
},
},
}}
component={PokemonIndex}
/>
<Stack.Screen
name="PokemonDetails"
options={{
headerTitle: 'Pokemon Details',
}}
component={PokemonDetails}
/>
</Stack.Navigator>
</PokemonContext.Provider>
</NavigationContainer>
);
}
export default App;