Uma*_*ruq 31 react-native react-navigation
当我使用 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);
Run Code Online (Sandbox Code Playgroud)
之前我使用过 React navigation v5 并使用该方法进行查找
感谢您的帮助
小智 46
也许你想做这样的事情:
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>
);
};
Run Code Online (Sandbox Code Playgroud)
然后在你的代码中做这样的事情:
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>
);
};
Run Code Online (Sandbox Code Playgroud)
amb*_*ssi 19
这是因为您必须指定此类型,因为这将确保类型安全。
解决方案:
1 - 类型检查导航器
// root.routes.tsx
import { createStackNavigator } from '@react-navigation/stack';
export type RootStackParamList = {
Home: undefined;
Profile: { userId: string };
Feed: { sort: 'latest' | 'top' } | undefined;
};
Run Code Online (Sandbox Code Playgroud)
const RootStack = createStackNavigator<RootStackParamList>();
Run Code Online (Sandbox Code Playgroud)
2 - 为根导航器指定全局类型
// navigation.d.ts
import { RootStackParamList } from '../routes/root.routes';
declare global {
namespace ReactNavigation {
interface RootParamList extends RootStackParamList { }
}
}
Run Code Online (Sandbox Code Playgroud)
3 - 使用它!
import { useNavigation } from '@react-navigation/native';
const navigation = useNavigation();
navigation.navigate('Home');
Run Code Online (Sandbox Code Playgroud)
4-额外!如果需要获取参数:
import { useRoute, RouteProp } from '@react-navigation/native';
import { RootStackParamList } from '../routes/root.routes';
const route = useRoute<RouteProp<RootStackParamList, 'Profile'>>();
const id = route.params.userId;
Run Code Online (Sandbox Code Playgroud)
参考文献:
Jos*_*nça 12
我解决了这个问题,现在会有帮助,但这不是正确的输入:
type Nav = {
navigate: (value: string) => void;
}
const { navigate } = useNavigation<Nav>()
function foo() {
navigate("Home")
}
Run Code Online (Sandbox Code Playgroud)
虽然其他答案涵盖了很多内容,但我想我可以通过描述钩子中的用法useNavigation以及将{navigation}through 属性传递到屏幕来补充这一点。
首先,设置堆栈导航器:
/**
* Types for Stack Navigator.
*/
export type StackParamList = {
Main: undefined;
Home: undefined;
};
const Stack = createStackNavigator<StackParamList>();
Run Code Online (Sandbox Code Playgroud)
useNavigation钩子时:import { StackNavigationProp } from "@react-navigation/stack";
/**
* Types for the Stack Navigator.
*/
export type StackNavigation = StackNavigationProp<StackParamList>;
const navigation = useNavigation<StackNavigation>();
Run Code Online (Sandbox Code Playgroud)
/**
* Types for passing the navigation props to screens in the Bottom Tab Navigator.
*/
export type StackNavigationProps = {
navigation: StackNavigation;
};
const SomeScreenInTheStack = ({ navigation }: StackNavigationProps) => {
...
}
Run Code Online (Sandbox Code Playgroud)
希望这对某人有用!
| 归档时间: |
|
| 查看次数: |
38230 次 |
| 最近记录: |