React-Native不同屏幕之间基本路由跳转
Posted 孙焱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React-Native不同屏幕之间基本路由跳转相关的知识,希望对你有一定的参考价值。
App.js:
import React from \'react\' import { StyleSheet, Text, View } from \'react-native\' import {NavigationContainer} from \'@react-navigation/native\' import {createStackNavigator} from \'@react-navigation/stack\' import Home from \'./pages/Home\' import Detail from \'./pages/Detail\' const Stack = createStackNavigator(); export default function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={Home} options={{title:\'这是Home页面\'}}></Stack.Screen> <Stack.Screen name="Detail" component={Detail} options={{title:\'这是Detail页面\'}}></Stack.Screen> </Stack.Navigator> </NavigationContainer> ) } const styles = StyleSheet.create({})
Home.js:
import React from \'react\' import { StyleSheet, Text, View,Button} from \'react-native\' export default function Home({navigation}) { return ( <View> <Text style={styles.Title}>Home</Text> <View style={styles.Btn}> <Button title="点击到Detail页面" onPress={()=>{navigation.navigate(\'Detail\')}}></Button> </View> </View> ) } const styles = StyleSheet.create({ Btn:{ marginTop:20, width:300, height:40, left:"10%" }, Title:{ color:\'red\', fontSize:28, textAlign:\'center\' } })
Detail.js:
import React from \'react\'; import {StyleSheet, Text, View,Button} from \'react-native\'; export default function Detail({navigation}) { return ( <View> <Text style={styles.Title}>Detail</Text> <View style={styles.Btn}> <Button title="点击到Home页面" onPress={() => { navigation.navigate(\'Home\'); }}></Button> </View> </View> ); } const styles = StyleSheet.create({ Btn: { marginTop: 20, width: 300, height: 40, left: \'10%\', }, Title: { color: \'red\', fontSize: 28, textAlign: \'center\', }, });
以上是关于React-Native不同屏幕之间基本路由跳转的主要内容,如果未能解决你的问题,请参考以下文章