ReactNative Expo 急速教程(3) - 多页切换

多页应用有多种选择,但是官方推荐的是React Navigation。本文会做一个如下的app。

首先安装一堆依赖:

expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view

然后安装组件:

npm install @react-navigation/stack -S

代码如下:

import * as React from 'react';

import { View, Text ,Button} from 'react-native';

import { NavigationContainer } from '@react-navigation/native';

import { createStackNavigator } from '@react-navigation/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>

);

}

const Stack = createStackNavigator();

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;

代码分析,明天再说 。

以上是 ReactNative Expo 急速教程(3) - 多页切换 的全部内容, 来源链接: utcz.com/a/36151.html

回到顶部