【问题标题】:React navigation drawer content props types definitionReact 导航抽屉内容道具类型定义
【发布时间】:2020-07-09 22:04:24
【问题描述】:

我正在使用this guide 创建自定义抽屉内容:

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

效果很好,但我想对props 参数进行类型检查。所以我尝试了:

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

const DrawerContent = (props: DrawerContentComponentProps) => (
  // Same stuff
);

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

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

【问题讨论】:

  • 上面的代码很适合我。请澄清“我的 IDE 现在告诉我”是什么意思?确切的错误信息是什么?报告哪一行包含错误?在我看来,这是 调用 DrawerContent 使用不完整参数的东西。
  • “NavigationHelpers, DrawerNavigationEventMap>”类型上不存在属性“closeDrawer”。ts(2339)

标签: reactjs typescript react-native react-navigation


【解决方案1】:

这是 react-navigation 库的一个已知问题: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())}
/>

【讨论】:

  • 这是import { DrawerActions } from '@react-navigation/native'; 的答案,我一直在使用from '@react-navigation/drawer',但没有用。非常感谢
猜你喜欢
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多