gpt4 book ai didi

react-native - 如何在 React Navigation 中使用标题按钮导航到其他页面?

转载 作者:行者123 更新时间:2023-12-02 16:27:34 24 4
gpt4 key购买 nike

如何使用标题上的按钮在屏幕之间导航?当我尝试使用标题按钮返回或导航到其他页面时,我遇到了同样的错误。屏幕上的按钮工作正常。

“undefined 不是一个对象(评估'navigation.navigate')(设备)”

“undefined 不是一个对象(评估‘navigation.goBack’)”

import 'react-native-gesture-handler';
import React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import Constants from 'expo-constants';

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

const Stack = createStackNavigator();

//Screen 1
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Details"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
}

//Screen 2
function DetailsScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details</Text>
<Button title="back home" onPress={() => navigation.goBack()} />
</View>
);
}

export default function App({ navigation, route }) {
return (
<NavigationContainer>
<Stack.Navigator initialRout="Home">
<Stack.Screen name="Home" component={HomeScreen} />


<Stack.Screen
name="Details"
component={DetailsScreen}
options={{
headerTitle: () => <Text>Register</Text>,
headerRight: () => <Button title="Test" onPress={() => navigation.navigate('Home')} />,
headerLeft: () => <Button title="back home" onPress={() => navigation.goBack()} />,
}}
/>


</Stack.Navigator>
</NavigationContainer>
);
}

https://snack.expo.io/@camileppst/navigation-test

最佳答案

您需要注入(inject)导航 Prop ,而不是从 App 组件的 Prop 中获取:

export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRout="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen
name="Details"
component={DetailsScreen}
options={({navigation}) => ({
headerTitle: () => <Text>Register</Text>,
headerRight: () => <Button title="Test" onPress={() => navigation.navigate('Home')} />,
headerLeft: () => <Button title="back home" onPress={() => navigation.goBack()} />,
})}
/>
</Stack.Navigator>
</NavigationContainer>
);
}

关于react-native - 如何在 React Navigation 中使用标题按钮导航到其他页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64195199/

24 4 0
文章推荐: python - 从类 __init__() 调用静态方法导致 "takes 1 positional argument but 2 were given"TypeError
文章推荐: angular - 错误 :Property 'map' does not exist on type 'Observable