首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将React-Day-Picker中的DayPickerInput与TypeScript一起使用

无法将React-Day-Picker中的DayPickerInput与TypeScript一起使用
EN

Stack Overflow用户
提问于 2017-07-04 19:14:57
回答 3查看 1.2K关注 0票数 1

每当我尝试导入DayPickerInput时,如下所示:

代码语言:javascript
复制
import DayPickerInput from 'react-day-picker/DayPickerInput';

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

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

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

代码语言:javascript
复制
var DayPickerInput = require('react-day-picker').DayPickerInput;

我的项目构建得很好,但当组件需要显示时,我在控制台中得到了2个运行时错误:

代码语言:javascript
复制
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。

EN

回答 3

Stack Overflow用户

发布于 2017-10-06 03:34:59

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

票数 0
EN

Stack Overflow用户

发布于 2018-02-14 22:04:53

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

代码语言:javascript
复制
declare module 'react-day-picker/DayPickerInput' {
  import { DayPickerInput } from 'react-day-picker/types/DayPickerInput';
  export default DayPickerInput;
}

我的印象是TypeScript的定义不是最新的,并且有人(你或我?)需要更新吗?:)

票数 0
EN

Stack Overflow用户

发布于 2021-01-11 02:00:41

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

导入的可视代码:

代码语言:javascript
复制
  import DayPickerInput from 'react-day-picker/types/DayPickerInput';

正确的导入:

代码语言:javascript
复制
  import DayPickerInput from 'react-day-picker/DayPickerInput';

发生此问题的版本:

package.json (相关包)

代码语言:javascript
复制
  "dependencies": {
    { ... },
    "react-day-picker": "7.4.8",
  },
  "devDependencies": {
    { ... },
    "@types/react-day-picker": "5.3.0",
    "typescript": "4.1.3",
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44904486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档