【问题标题】:Unable to use DayPickerInput from React-Day-Picker with TypeScript无法将 React-Day-Picker 中的 DayPickerInput 与 TypeScript 一起使用
【发布时间】:2017-07-04 11:14:56
【问题描述】:

每当我尝试像下面这样导入 DayPickerInput 时:

import DayPickerInput from 'react-day-picker/DayPickerInput';

我收到 ts 警告:“找不到模块 'react-day-picker/DayPickerInput 的声明文件。”

查看模块文件夹,似乎只有 DayPicker 定义了类型。

当我尝试使用 require 方法时,如下所示:

var DayPickerInput = require('react-day-picker').DayPickerInput;

我的项目构建良好,但是当需要显示组件时,控制台中出现 2 个运行时错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. 

DayPickerInput 目前对我来说无法使用。

【问题讨论】:

标签: reactjs typescript react-day-picker


【解决方案1】:

react-day-picker v6.2 为输入组件添加了 TypeScript 定义。尝试升级,看看它是否能解决您的问题。

【讨论】:

  • 我有 v6.2.1,这对我来说仍然是个问题。 DayPickerInput 的文件位于 types 文件夹中,但由于某种原因它没有被 typescript 拾取。
  • 这是我遇到的问题的沙箱:codesandbox.io/s/98o50qmn8y 虽然沙箱中的错误是错误的。我得到的错误是“找不到模块'react-day-picker/DayPickerInput 的声明文件”。就像上面一样。我试着做一个 PR 来解决这个问题,但我看到其他人已经做了,你拒绝了它而支持你现在拥有的东西,这是行不通的。
【解决方案2】:

在 7.0.7 版本中,我不得不将此添加到 globals.d.ts 作为解决方法:

declare module 'react-day-picker/DayPickerInput' {
  import { DayPickerInput } from 'react-day-picker/types/DayPickerInput';
  export default DayPickerInput;
}

我的印象是 TypeScript 定义不是最新的,有人(你还是我?)需要更新它们? :)

【讨论】:

    【解决方案3】:

    解决方案:如果您也遇到此问题,请检查您的导入中是否存在 react-day-picker 和 DayPickerInput 之间的“类型”。

    导入了哪些可视化代码:

      import DayPickerInput from 'react-day-picker/types/DayPickerInput';
    

    正确的导入:

      import DayPickerInput from 'react-day-picker/DayPickerInput';
    

    出现此问题的版本:

    package.json(相关包)

      "dependencies": {
        { ... },
        "react-day-picker": "7.4.8",
      },
      "devDependencies": {
        { ... },
        "@types/react-day-picker": "5.3.0",
        "typescript": "4.1.3",
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多