简体   繁体   English

在组件屏幕道具中反应 typescript 的导航类型

[英]react navigation type for typescript in component screen props

I have a warning or error for navigation props in component screen我对组件屏幕中的navigation道具有警告或错误

export default function FoodMenuList({ navigation }) {}

because I dont assign type for navigation因为我没有为navigation指定类型

Binding element 'navigation' implicitly has an 'any'

I tried to search navigation props type for react navigation, but I dont find我试图搜索导航道具类型以进行反应导航,但我没有找到

what is react-navigation navigation props type? react-navigation导航道具类型是什么?

or may be?或者可能?

export default function FoodMenuList({ navigation }: {navigation: {navigate:Function}}) {}

or may be there is better solution?或者可能有更好的解决方案?

try the useNavigation hook from @react-navigation/native , check the docs useNavigation尝试来自@react-navigation/nativeuseNavigation钩子,检查文档useNavigation

useNavigation is a hook which gives access to navigation object. useNavigation 是一个钩子,它可以访问导航 object。 It's useful when you cannot pass the navigation prop into the component directly, or don't want to pass it in case of a deeply nested child.当您无法将导航道具直接传递到组件中时,它很有用,或者在嵌套深度嵌套的孩子的情况下不想传递它。

you can also write你也可以写

interface Props {
  navigation: NavigationType;
}

FoodMenuList({ navigation }: Props)

if you're using class component or you don't wont to use useNavigation如果您使用的是 class 组件,或者您不会使用useNavigation

According to the documentation provided by react-navigation https://reactnavigation.org/docs/typescript/#type-checking-screens根据 react-navigation https://reactnavigation.org/docs/typescript/#type-checking-screens提供的文档

To type check our screens, we need to annotate the navigation prop and the route prop received by a screen.要对屏幕进行类型检查,我们需要对屏幕接收到的导航道具和路线道具进行注释。 The navigator packages in React Navigation export a generic types to define types for both the navigation and route props from the corresponding navigator. React Navigation 中的 navigator 包导出一个泛型类型,以从相应的 navigator 定义 navigation 和 route props 的类型。

For example, you can use NativeStackScreenProps for the Native Stack Navigator.例如,您可以将 NativeStackScreenProps 用于 Native Stack Navigator。

import type { NativeStackScreenProps } from '@react-navigation/native-stack';

type RootStackParamList = {
  Home: undefined;
  Profile: { userId: string };
  Feed: { sort: 'latest' | 'top' } | undefined;
};

type Props = NativeStackScreenProps<RootStackParamList, 'Profile'>;

This allows us to type check route names and params which you're navigating using navigate, push etc. The name of the current route is necessary to type check the params in route.params and when you call setParams.这允许我们键入检查路线名称和您正在使用导航、推送等导航的参数。当前路线的名称对于键入检查 route.params 中的参数以及调用 setParams 时是必需的。

Then you can use the Props type you defined above to annotate your component.然后你可以使用上面定义的 Props 类型来注释你的组件。

For function components:对于 function 组件:

function ProfileScreen({ route, navigation }: Props) {
  // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM