探索React Native与Redux Navigation的完美结合:一个实战示例
warning:
这篇文章距离上次修改已过199天,其中的内容可能已经有所变动。
import React from 'react';
import { View, Text, Button } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
// 定义导航器
const Stack = createStackNavigator();
// 定义一个简单的屏幕组件
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>
);
}
function DetailsScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button
title="Go to Details again"
onPress={() => navigation.push('Details')}
/>
<Button
title="Go back"
onPress={() => navigation.goBack()}
/>
</View>
);
}
// 应用的根组件
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
这个代码实例展示了如何使用React Navigation库和React Native创建一个简单的应用,其中包含了导航功能和按钮交互。这个例子简单明了,有助于初学者理解和入门React Native开发。
评论已关闭