【发布时间】: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