Created
August 16, 2018 14:09
-
-
Save chongiscool/4a013a69bcc1e7042df59883211e38ad to your computer and use it in GitHub Desktop.
Passing parameters to routes, after create a stack navigator with some routes and navigate between those routes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { | |
StyleSheet, | |
View, | |
Text, | |
Platform, | |
TouchableOpacity, Button | |
} from 'react-native' | |
import { Ionicons, FontAwesome } from '@expo/vector-icons' | |
import { | |
createBottomTabNavigator, | |
createMaterialTopTabNavigator, | |
createStackNavigator, | |
createDrawerNavigator, | |
TabBarBottom, tabBarIcon } from 'react-navigation' | |
import Detail from './Detail' | |
export default class App extends React.Component { | |
render() { | |
return( | |
<RootStack /> | |
) | |
} | |
} | |
const Home = ({ navigation }) => ( | |
<View style={styles.container}> | |
<Text style={styles.text}>Home Screen!</Text> | |
/* 1.Navigate to the Details route with params */ | |
<TouchableOpacity onPress={() => navigation.navigate('Detail', {itemId: 86, otherParam: 'anything you want here',})}> | |
<Text style={styles.detailText}>Go to Detail</Text> | |
</TouchableOpacity> | |
</View> | |
); | |
const RootStack = createStackNavigator({ | |
Home: Home, | |
Detail: Detail | |
},{ | |
initialRouteName: 'Home', | |
}) | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: 'center', | |
justifyContent: 'center', | |
marginLeft: 10, | |
marginRight: 10 | |
}, | |
text: { | |
color: 'red' | |
}, | |
detailText: { | |
color: 'blue' | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { | |
StyleSheet, | |
View, | |
Text, | |
Platform, | |
TouchableOpacity, Button | |
} from 'react-native' | |
import { Ionicons, FontAwesome } from '@expo/vector-icons' | |
import { | |
createBottomTabNavigator, | |
createMaterialTopTabNavigator, | |
createStackNavigator, | |
createDrawerNavigator, | |
TabBarBottom, tabBarIcon } from 'react-navigation' | |
class Detail extends Component { | |
render() { | |
/* Get the param, provide a fallback value if not available */ | |
const { navigation } = this.props | |
const itemId = navigation.getParam('itemId', 'No-Id') | |
const otherParam = navigation.getParam('otherParam', 'some default values') | |
return( | |
<View style={styles.container}> | |
<Text>Details Screen!</Text> | |
<Text>itemId: {JSON.stringify(itemId)}</Text> | |
<Text>otherParam: {JSON.stringify(otherParam)}</Text> | |
<TouchableOpacity onPress={() => navigation.navigate('Home')}> | |
<Text style={styles.detailText}>Go to Home</Text> | |
</TouchableOpacity> | |
<Text style={{fontSize: 30}}>---------</Text> | |
<TouchableOpacity onPress={() => navigation.push('Detail', { | |
itemId: Math.floor(Math.random() * 100), | |
})}> | |
<Text style={styles.detailText}>Go to Detail again</Text> | |
</TouchableOpacity> | |
<Text style={{fontSize: 30}}>---------</Text> | |
<TouchableOpacity onPress={() => navigation.goBack()}> | |
<Text style={styles.detailText}>Go Back</Text> | |
</TouchableOpacity> | |
</View> | |
) | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: 'center', | |
justifyContent: 'center', | |
marginLeft: 10, | |
marginRight: 10 | |
}, | |
text: { | |
color: 'red' | |
}, | |
detailText: { | |
color: 'blue' | |
} | |
}) | |
export default Detail; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are two pieces to this:
navigation.navigate
function:this.props.navigation.navigate('RouteName', { /* params go here */ })
this.props.navigation.getParam(paramName, defaultValue).
reference source: reactnavigation/docs/params