首页 > 其他 > 详细

React-Native不同屏幕之间基本路由跳转

时间:2021-05-27 17:03:20      阅读:27      评论:0      收藏:0      [点我收藏+]

App.js:

 1 import React from ‘react‘
 2 import { StyleSheet, Text, View } from ‘react-native‘
 3 import {NavigationContainer} from ‘@react-navigation/native‘
 4 import {createStackNavigator} from ‘@react-navigation/stack‘
 5 
 6 import Home from ‘./pages/Home‘
 7 import Detail from ‘./pages/Detail‘
 8 
 9 const Stack = createStackNavigator();
10 
11 export default function App() {
12   return (
13    <NavigationContainer>
14      <Stack.Navigator>
15        <Stack.Screen name="Home" component={Home} headerMode=‘none‘></Stack.Screen>
16        <Stack.Screen name="Detail" component={Detail} headerMode=‘none‘></Stack.Screen>
17      </Stack.Navigator>
18    </NavigationContainer>
19   )
20 }
21 
22 const styles = StyleSheet.create({})

 

 

Home.js:

技术分享图片
 1 import React from ‘react‘;
 2 import {StyleSheet, Text, View, Button} from ‘react-native‘;
 3 
 4 export default function Home({navigation}) {
 5   return (
 6     <View>
 7       <Text style={styles.Title}>Home</Text>
 8       <View style={styles.Btn}>
 9         <Button
10           title="点击到Detail页面"
11           onPress={() => {
12             navigation.push(‘Detail‘,{name:‘孙焱‘});
13           }}></Button>
14       </View>
15     </View>
16   );
17 }
18 
19 const styles = StyleSheet.create({
20   Btn: {
21     marginTop: 20,
22     width: 300,
23     height: 40,
24     left: ‘10%‘,
25   },
26   Title: {
27     color: ‘red‘,
28     fontSize: 28,
29     textAlign: ‘center‘,
30   },
31 });
View Code

 

Detail.js:

技术分享图片
 1 import React from ‘react‘;
 2 import {StyleSheet, Text, View,Button} from ‘react-native‘;
 3 
 4 export default function Detail({route,navigation}) {
 5   const {name} = route.params;
 6   return (
 7     <View>
 8       <Text>{name}</Text>
 9       <Text style={styles.Title}>Detail</Text>
10       <View style={styles.Btn}>
11         <Button
12           title="点击到Home页面"
13           onPress={() => {
14             navigation.navigate(‘Home‘);
15           }}></Button>
16       </View>
17     </View>
18   );
19 }
20 
21 const styles = StyleSheet.create({
22   Btn: {
23     marginTop: 20,
24     width: 300,
25     height: 40,
26     left: ‘10%‘,
27   },
28   Title: {
29     color: ‘red‘,
30     fontSize: 28,
31     textAlign: ‘center‘,
32   },
33 });
View Code

 

React-Native不同屏幕之间基本路由跳转

原文:https://www.cnblogs.com/sunyan97/p/14817591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!