yarn add react-native-screens
yarn add react-native-safe-area-context
yarn add @react-navigation/native-stack
从 React Native 0.60 及更高版本开始,。所以你不需要运行 react-native link
。
npx pod-install ios
react-native-screens
软件包需要一个额外的配置步骤才能在 Android 设备上正常工作。
MainActivity.java
位于android/app/src/main/java/<你的项目名称>/MainActivity.java
.
将下面代码添加到MainActivity
类的主体中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
}
并确保在此文件顶部的包声明下方添加以下导入声明:
import android.os.Bundle;
App.js
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
function DetailsScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
现在我们的堆栈有两条路线,一条Home
路线和一条Details
路线。可以使用组件来指定路线Screen
。该Screen
组件接受一个name
与我们将用于导航的路线名称相对应的 prop,以及一个component
与它将渲染的组件相对应的 prop。
这里,Home
路由对应组件HomeScreen
,Details
路由对应组件DetailsScreen
。堆栈的初始路由就是Home
路由。尝试将其更改为Details
并重新加载应用程序(React Native 的快速刷新不会更新 的更改initialRouteName
,如您所料),请注意您现在将看到屏幕Details
。然后将其更改回Home
并再次重新加载。
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
}
1)传参
navigation.navigate('Details', {
itemId: 86,
otherParam: 'anything you want here',
})
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
navigation.navigate('Details', {
itemId: 86,
otherParam: 'anything you want here',
});
}}
/>
</View>
);
}
1)读取
function DetailsScreen({ route, navigation }) {
const { itemId, otherParam } = route.params; // 读取参数
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Text>itemId: {JSON.stringify(itemId)}</Text>
<Text>otherParam: {JSON.stringify(otherParam)}</Text>
<Button
title="Go to Details... again"
onPress={() =>
navigation.push('Details', {
itemId: Math.floor(Math.random() * 100),
})
}
/>
<Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
<Button title="Go back" onPress={() => navigation.goBack()} />
</View>
);
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务