react-native react-navigation6.x 路由跳转及传参
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import React from 'react';
import { Button } from 'react-native';
// 定义导航器
const Stack = createStackNavigator();
// 定义两个屏幕
function HomeScreen({ navigation }) {
return (
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details', { itemId: 123 })}
/>
);
}
function DetailsScreen({ route, navigation }) {
// 获取传递的参数
const { itemId } = route.params;
// 使用参数
return (
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details', { itemId: itemId + 1 })}
/>
);
}
// 导出应用
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
这个例子展示了如何在React Native应用中使用React Navigation 6.x进行路由跳转及参数传递。HomeScreen
组件中使用navigation.navigate
方法跳转到DetailsScreen
,并传递了一个名为itemId
的参数。DetailsScreen
组件接收到route.params
后使用该参数,并且可以继续跳转并传递新的参数。
评论已关闭