reactjs - React 抽屉导航内容 Prop 类型定义

标签 reactjs typescript react-native react-navigation

我正在使用 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 不存在,但它确实存在。

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

最佳答案

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

关于reactjs - React 抽屉导航内容 Prop 类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915266/

相关文章:

javascript - 如何从类组件更新功能组件的状态,以便在 react 中显示 map 中的项目

reactjs - 通过使用样式化组件,如何为来自外部 Prop 的背景颜色添加一些不透明度?

typescript - Vue 3 Typescript ComputedRef 问题

javascript - 如何在 React Native 中渲染 BULLET 字符?

reactjs - React Spring "Can' t 在未安装的组件上执行 React 状态更新”

reactjs - 由于 'Type '元素,React TypeScript无法对表进行排序元素'无法分配给 'string' .ts(2345)'类型

javascript - 将 HTTP 响应类型设置为 arraybuffer

javascript - 如何在 .tsx typescript 中包含 .css 文件?

java - 在react-native run-android期间安装Gradle时出现SSL错误

react-native - 从 React Navigation 更改事件边框颜色 - MaterialTopTabNavigator