【问题标题】:React datepicker and typescript反应日期选择器和打字稿
【发布时间】:2020-01-14 13:39:06
【问题描述】:

目前正在尝试将 React 和 Typescript 与 react-datepicker 一起使用。

我有一个想要测试的简单设置,但我不断收到以下错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

下面是使用DatePicker包的文件

import * as React from 'react';
import DatePicker from "react-datepicker";

import "react-datepicker/dist/react-datepicker.css";

interface DateConstructor  {
    startDate: Date;
} 

export class DateSelector extends React.Component<{}, DateConstructor > {
    constructor(props) {
        super(props);
        this.state = {
            startDate: new Date()
        };
    }

    private handleChange(date) {
        this.setState({
            startDate: date
        });
    }

    public render() {
        const { startDate } = this.state;
        return (
            <DatePicker 
                dateFormat="dd-mm-yyyy"
                selected={startDate} 
                onChange={this.handleChange}
            />
        )
    }
}

DatePicker 包需要一个字符串或函数对我来说似乎很奇怪?

如何解决这个问题?

以下完全错误

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
invariant
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:118:15
ReactCompositeComponentWrapper.instantiateReactComponent
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:20273:23
ReactCompositeComponentWrapper.performInitialMount
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29799:22
ReactCompositeComponentWrapper.mountComponent
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29690:21
Object.mountComponent
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:12868:35
ReactCompositeComponentWrapper.performInitialMount
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29803:34
ReactCompositeComponentWrapper.mountComponent
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29690:21
Object.mountComponent
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:12868:35
ReactCompositeComponentWrapper.performInitialMount
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29803:34
ReactCompositeComponentWrapper.mountComponent
http://localhost:65273/dist/vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29690:21

下面是我尝试渲染 DateSelector 组件的文件

import * as React from 'react';
import { RouteComponentProps } from 'react-router';

import { DataOutputSwitch } from './ComponentLibrary/DataOutputSwitch';
import { DatatypeSelector } from './ComponentLibrary/DatatypeSelector';
import { DateSelector } from './ComponentLibrary/DateSelector';


export class ComponentLibrary extends React.Component<RouteComponentProps<{}>, {}> {

    public render() {
        return (
            <div>
                <h1>Pair4Insights Component Library</h1>
                <p>This component demonstrates all separate components.</p>
                <div style={{display: 'flex', flexDirection: 'column'}}>
                    <h3>Data Output Switch</h3>
                    <DataOutputSwitch />
                    <h3>Datattype Selector</h3>
                    <DatatypeSelector datatype="revenue" />
                    <h3>Date Selector</h3>
                    <DateSelector />
                </div>
            </div>
        );
    }
}

【问题讨论】:

  • 你可以分享你正在使用这个功能的文件吗?
  • 问题已更新为完整文件!
  • 您的onChange 处理程序似乎无法正常工作。通过render 方法将其记录到控制台,检查它是否更新了状态。如果没有,请在构造函数中使用箭头函数或.bind
  • 我已将onChange 设置为console.log('test') 以测试它是否是函数。仍然出现同样的错误。

标签: reactjs typescript react-datepicker


【解决方案1】:

我将这个组件插入到一个正常工作的 Typescript React 应用程序中,并稍作修改,它可以正常工作。所以我怀疑这个问题是由这个组件以外的其他因素引起的。如果您注释掉这个组件而不导入它,这个错误会消失吗?

为了参考,代码如下:

import * as React from 'react';
import DatePicker from "react-datepicker";

import "react-datepicker/dist/react-datepicker.css";

interface DateConstructor  {
    startDate: Date;
} 

export class DateSelector extends React.Component<{}, DateConstructor> {
    constructor(props) {
        super(props);
        this.state = {
            startDate: new Date()
        };
        this.handleChange = this.handleChange.bind(this);
    }

    private handleChange(date) {
        console.log('date is here!', date);
        this.setState({
            startDate: date
        });
    }

    public render() {
        const { startDate } = this.state;
        return (
            <DatePicker
                dateFormat="dd/MM/yyyy"
                selected={startDate} 
                onChange={this.handleChange}
            />
        )
    }
}

以及App.tsx 的导入:

import { DateSelector } from './DateSelector';

如您所见,我所做的一些更改是绑定handleChange 函数并更新日期格式(在问题代码中,这是一个不正确的输入)。

我希望这个答案能将你推向正确的方向。

【讨论】:

  • 嗨!是的,当我注释掉组件的呈现时,错误就会消失。
  • @Pimmesz 嗯,你的工作文件夹中的 datepicker 包本身可能有问题吗?例如,您是否尝试过使用npm ci(或您首选的包管理器等效项)重新安装所有内容?除此之外,我认为有太多的可能性来猜测哪里会出错。
【解决方案2】:

selected prop 期望获得一个字符串作为默认选择值。您正在传递一个日期对象。您可以使用useState 挂钩来维护状态,例如:

import {useState} from "react";

const YourComponent = () => {
   const [date, setDate] = useState("");

   return (
      <DatePicker 
          dateFormat="DD-MM-YYYY"
          selected={date} 
          onChange={date => setDate(date)}
       />
   );
}

【讨论】:

  • 好吧,即使给选定的值null 也是可能的。仍然得到同样的错误。如果我将日期转换为string,我也会收到错误:Invalid time value。所以 selected 需要一个对象。
  • 通过说错误堆栈,我假设您共享一些行号参考以进行调查
  • 这是我收到的完整错误。它没有说任何关于特定行的内容。这就是让这个问题变得格外困难的原因。
  • 你能告诉我你正在使用的DateSelector 的导入吗?
  • 我已经添加了我尝试在问题中呈现DateSelector 组件的文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 2018-09-29
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
相关资源
最近更新 更多