1

每当我尝试像下面这样导入 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 现在对我来说无法使用。

4

3 回答 3

0

解决方案:如果您也遇到此问题,请检查您的导入中是否有“类型”,位于 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",
  }
于 2021-01-10T18:00:40.713 回答
0

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

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

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

于 2018-02-14T14:04:53.310 回答
0

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

于 2017-10-05T19:34:58.730 回答