import * as React from 'react'; import type {PropsWithChildren} from 'react'; import {View, Text, Button, StyleSheet, TouchableHighlight} from 'react-native'; import {NavigationContainer} from '@react-navigation/native'; import {createNativeStackNavigator} from '@react-navigation/native-stack';
type SectionProps = PropsWithChildren<{ extraData?: string; navigation: any; route: any; }>;
function HomeScreen({ extraData, navigation, route, }: SectionProps): React.JSX.Element { const [postParams, setParams] = React.useState('我是默认值');
React.useEffect(() => { if (route.params?.post) { console.log(route.params.post); setParams(route.params.post); } }, [route.params?.post]); return ( <View style={{ flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', }}> <Text>Home Screen{extraData}</Text> <Text>{postParams}</Text> <Button title="Go to Details" onPress={() => /* 1. 使用参数传参到Details页面 */ navigation.navigate('Details', { itemId: Math.floor(Math.random() * 100), otherParam: 'anything you want here', }) } /> </View> ); }
function DetailsScreen({navigation, route}: SectionProps): React.JSX.Element { const styles = StyleSheet.create({ ButtonBack: { marginTop: 20, }, button: { backgroundColor: '#007bff', padding: 10, borderRadius: 5, marginTop: 20, }, text: { color: '#ffffff', textAlign: 'center', }, });
const {itemId, otherParam} = route.params;
return ( <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}> <Text>Details Screen0</Text> <Text>itemId: {JSON.stringify(itemId)}</Text> <Text>otherParam: {JSON.stringify(otherParam)}</Text> <View style={[styles.ButtonBack]}> <Button title="Go to Home" onPress={() => navigation.navigate('Home')} /> </View> <View style={[styles.ButtonBack]}> <Button color={'orange'} title="Go back" onPress={() => navigation.goBack()} /> </View> {/* 自定义按钮 */} <TouchableHighlight style={styles.button} underlayColor={'#0069d9'} onPress={() => alert('按钮被点击了')}> <Text style={styles.text}>点击我(自定义按钮)</Text> </TouchableHighlight> <View style={[styles.ButtonBack]}> {/* 3.更新传过来的参数值 */} <Button title="更新参数" onPress={() => navigation.setParams({ otherParam: 'someText', }) } /> </View> <View style={[styles.ButtonBack]}> {/* 4.回传参数 把Home的参数回传过去 */} <Button title="回传参数" onPress={() => { // Pass and merge params back to home screen navigation.navigate({ name: 'Home', params: {post: '我是详情页回传的参数'}, merge: true, }); }} /> </View> </View> ); }
const Stack = createNativeStackNavigator();
function App() { const [someData] = React.useState('some data'); return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" options={{ title: 'Home', headerStyle: { backgroundColor: '#f1f1f1', //顶部标题背景 }, headerTintColor: '#000', headerShadowVisible: false, //是否显示阴影 headerTitleAlign: 'center', //居中还是left headerTitleStyle: { fontWeight: 'bold', color: '#000', }, headerRight: () => ( <Button onPress={() => alert('This is a button!')} title="Info" color="blue" /> ), }}> {props => <HomeScreen {...props} extraData={someData} />} </Stack.Screen> <Stack.Screen name="Details" component={DetailsScreen} // 在标题中使用params options={({route}: any) => ({ title: 'Details-' + route?.params?.itemId, headerStyle: { backgroundColor: '#fff', //顶部标题背景 }, })} // 默认参数如果没有传默认值 initialParams={{itemId: 42}} /> </Stack.Navigator> </NavigationContainer> ); }
export default App;
|