当我使用 useNavigation 或 props { navigation } 使用 navigation.navigate('Home') 在屏幕之间导航时,打字稿返回错误 Argument of type '"Main"' is not allocate to type '{ key: string;参数?:未定义;合并?:布尔值 |不明确的; } | { 名称:从不;键?:字符串|不明确的;参数:从不;合并?:布尔值 |不明确的; }' 这是什么意思?
下面是我的代码:
import React from 'react';
import { View } from 'react-native';
import { Button } from 'react-native-paper';
import { useNavigation } from '@react-navigation/native';
const Home: React.FC = () => {
const navigation = useNavigation();
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button onPress={() => navigation.navigate('Main')}>Navigate</Button>
</View>
);
};
export default React.memo(Home);
之前我使用过 React navigation v5 并使用该方法进行查找
感谢您的帮助
也许你想做这样的事情:
export type RootStackParamList = {
Main: undefined;
Home: undefined;
};
const Stack = createStackNavigator<RootStackParamList>();
export const RootNavigator = () => {
return (
<Stack.Navigator initialRouteName="Main">
<Stack.Screen
name="Main"
component={Main}
/>
<Stack.Screen
name="Home"
component={Home}
/>
</Stack.Navigator>
);
};
然后在你的代码中做这样的事情:
type homeScreenProp = StackNavigationProp<RootStackParamList, 'Home'>;
const Home: React.FC = () => {
const navigation = useNavigation<homeScreenProp>();
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button onPress={() => navigation.navigate('Main')}>Navigate</Button>
</View>
);
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)