【发布时间】:2021-09-02 16:27:07
【问题描述】:
几个小时以来,我一直在尝试从 StackNavigation 导航到另一个 Stack 导航。
我正在使用打字稿,这并不容易为所有屏幕定义正确的类型。
我用下面的例子来说明我的错误。
在这种情况下,当我的应用程序启动时,第一个屏幕是ProfilHome。
在这个屏幕上我有两个按钮:
- 一个去我当前stackNavigation的另一个屏幕
ProfilInfo - 另一个去另一个堆栈导航
MarketStack和屏幕MarketInfo
当我点击第一个按钮时,没有问题。但是当我点击第二个按钮时,我得到了错误:
The action 'NAVIGATE' with payload {"name":"MarketInfo"} was not handled by any navigator.
Do you have a screen named 'MarketInfo'?
错误代码:
import * as React from 'react'
import {View, Text, Button} from 'react-native'
import { ReactElement } from 'react'
import {createStackNavigator, StackNavigationProp, StackScreenProps} from '@react-navigation/stack'
import {useNavigation} from "@react-navigation/native";
//**********************//
// Profile component
//**********************//
type ProfileNavigationStack = {
ProfilHome: undefined,
ProfilInfo: undefined,
}
type ProfileHomeNavProps = StackScreenProps<ProfileNavigationStack, 'ProfilHome'>
const ProfileStack = createStackNavigator<ProfileNavigationStack>()
const ProfileComponent = () => {
return (
<ProfileStack.Navigator>
<ProfileStack.Screen
options={{ headerTitle: 'ProfilHome' }}
name="ProfilHome"
component={ProfileHome}
/>
<ProfileStack.Screen
options={{ headerTitle: 'ProfilInfo' }}
name="ProfilInfo"
component={ProfileInfo}
/>
</ProfileStack.Navigator>
)
}
const ProfileHome = ({ navigation }: ProfileHomeNavProps) => {
const navToMarket = useNavigation<MarketHomeNavProps>()
return (
<View>
<Text>ProfileHome</Text>
<Button title={'goToInfo'} onPress={() => navigation.navigate('ProfilInfo')}/>
<Button title={'goToMarket'} onPress={() => navToMarket.navigate('MarketInfo')}/>
</View>
)
}
const ProfileInfo = () => (<View><Text>ProfileInfo</Text></View>)
//**********************//
// Market component
//**********************//
type MarketNavigationStack = {
MarketHome: undefined,
MarketInfo: undefined,
}
type MarketHomeNavProps = StackNavigationProp<MarketNavigationStack, 'MarketHome'>
const MarketStack = createStackNavigator<MarketNavigationStack>()
const MarketComponent = () => {
return (
<MarketStack.Navigator>
<MarketStack.Screen
options={{ headerTitle: 'ProfilHome' }}
name="MarketHome"
component={MarketHome}
/>
<MarketStack.Screen
options={{ headerTitle: 'MarketInfo' }}
name="MarketInfo"
component={MarketInfo}
/>
</MarketStack.Navigator>
)
}
const MarketHome = () => (<View><Text>MarketHome</Text></View>)
const MarketInfo = () => (<View><Text>MarketHome</Text></View>)
//**********************//
// Main component
//**********************//
type HomeNavigationStack = {
Profile: undefined,
Market: undefined,
}
const HomeStack = createStackNavigator<HomeNavigationStack>()
export default function StartApp(): ReactElement {
return (
<HomeStack.Navigator
screenOptions={{
headerShown: false,
}}
>
<HomeStack.Screen
name="Profile"
component={ProfileComponent}
/>
<HomeStack.Screen
name="Market"
component={MarketComponent}
/>
</HomeStack.Navigator>
)
}
我认为解决方案是here,但我不明白它是如何工作的。
有人可以帮我吗?
谢谢
【问题讨论】:
标签: typescript react-native react-navigation typescript-typings react-native-navigation