Skip to content

Commit ee1989b

Browse files
committed
💻 implement navigation using react-native-stack
1 parent bff93a1 commit ee1989b

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

‎App.js‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import 'react-native-gesture-handler'
12
import React, {useEffect, useState} from 'react';
23
import { StyleSheet, View } from 'react-native'; // React Native API Reference -> https://facebook.github.io/react-native/docs/activityindicator
3-
import { Header, ScrollableList } from './components'
4+
import { NavigationContainer } from '@react-navigation/native';
5+
import { createStackNavigator } from '@react-navigation/stack';
6+
7+
import { Header, ScrollableList, PokemonDetail } from './components'
48

59
const POKEMON_API = `https://pokeapi.co/api/v2/`
610
const GET_50_POKEMON_PATH = `pokemon?limit=50`
711

12+
const Stack = createStackNavigator();
13+
814
export default function App() {
915
const [pokemonList, setPokemonList] = useState([])
1016
// on componentDidMount grab list of pokemon and set the state to the list
@@ -18,10 +24,26 @@ export default function App() {
1824
}, [])
1925
console.log(pokemonList)
2026
return (
21-
<View style={styles.container}>
22-
<Header/>
23-
<ScrollableList list={pokemonList}/>
24-
</View>
27+
<NavigationContainer>
28+
<Stack.Navigator>
29+
<Stack.Screen
30+
name="Index"
31+
component={({navigation}) => { // Stack.Screen will inject a navigation prop to child components
32+
return(
33+
<View style={styles.container}>
34+
<Header/>
35+
<ScrollableList list={pokemonList} navigation={navigation}/>
36+
</View>
37+
)
38+
}}
39+
options={{title: 'Pokédex'}}
40+
/>
41+
<Stack.Screen
42+
name="Pokemon Detail"
43+
component={PokemonDetail}
44+
/>
45+
</Stack.Navigator>
46+
</NavigationContainer>
2547
);
2648
}
2749

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import { View, Text } from 'react-native'
3+
4+
export default function PokemonDetail() {
5+
return(
6+
<View>
7+
<Text>Hello</Text>
8+
</View>
9+
)
10+
}

‎components/ScrollableList/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react'
22

33
import { StyleSheet, SafeAreaView, ScrollView, Text, TouchableHighlight } from 'react-native'
4-
export default function ScrollableList({list}) {
4+
export default function ScrollableList({list, navigation}) {
55
return(
66
<SafeAreaView style={styles.container}>
77
<ScrollView>
88
{
99
list.map((pokemon, i) => (
1010
<TouchableHighlight
11-
onPress={() => console.log('pressed')}
11+
onPress={() => navigation.navigate('Pokemon Detail', {pokemon})}
1212
key={i}
1313
style={styles.listItem}>
1414
<Text style={styles.listItemText}>

‎components/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Header from './Header'
22
import ScrollableList from './ScrollableList'
3+
import PokemonDetail from './PokemonDetail'
34

45
export {
56
Header,
6-
ScrollableList
7+
ScrollableList,
8+
PokemonDetail
79
}

0 commit comments

Comments
 (0)