在React Native中,当你使用React Navigation库时,你可以通过路由参数向屏幕传递数据。如果你遇到了route.params
未定义的问题,这可能是由于以下原因:
navigation.navigate('TargetScreen', { key: 'value' });
import React from 'react';
import { View, Text } from 'react-native';
const TargetScreen = ({ route }) => {
const { key } = route.params;
return (
<View>
<Text>{key}</Text>
</View>
);
};
export default TargetScreen;
import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();
const AppNavigator = () => {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="TargetScreen" component={TargetScreen} />
</Stack.Navigator>
);
};
export default AppNavigator;
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import AppNavigator from './AppNavigator';
const App = () => {
return (
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
);
};
export default App;
领取专属 10元无门槛券
手把手带您无忧上云