当前正在尝试将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>
);
}
}
答案 0 :(得分:2)
我将该组件插入到工作正常的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
函数并更新日期格式(在问题代码中,这是错误的输入)。
我希望这个答案已将您推向正确的方向。
答案 1 :(得分:0)
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)}
/>
);
}