React 导航抽屉内容道具类型定义

Sou*_*euh 3 typescript reactjs react-native react-navigation

我正在使用本指南创建自定义抽屉内容:

const DrawerContent = (props) => (
  <DrawerContentScrollView {...props}>
    <AntDesign
      name="close"
      size={32}
      onPress={() => props.navigation.closeDrawer()}
    />
    <Text>TEST</Text>
    <DrawerItemList {...props} />
  </DrawerContentScrollView>
);
Run Code Online (Sandbox Code Playgroud)

它运行良好,但我想对props参数进行类型检查。所以我试过:

import { DrawerContentComponentProps } from '@react-navigation/drawer';

const DrawerContent = (props: DrawerContentComponentProps) => (
  // Same stuff
);
Run Code Online (Sandbox Code Playgroud)

但是我的 IDE 现在告诉我它props.navigation.closeDrawer不存在,但它确实存在。

定义函数props类型的正确方法是什么DrawerContent

Sou*_*euh 6

这是反应导航库的一个已知问题:https : //github.com/react-navigation/react-navigation/issues/6790

为了使警告消失等待修复,您可以使用以下符号:

import { DrawerActions } from '@react-navigation/native';


<AntDesign
  name="close"
  size={32}
  // @see https://github.com/react-navigation/react-navigation/issues/6790
  onPress={() => navigation.dispatch(DrawerActions.closeDrawer())}
/>
Run Code Online (Sandbox Code Playgroud)