gpt4 book ai didi

reactjs - react 日期选择器和 typescript

转载 作者:行者123 更新时间:2023-12-03 23:09:19 24 4
gpt4 key购买 nike

目前正在尝试将 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>
);
}
}

最佳答案

我将这个组件插入到一个正常工作的 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';
如您所见,我所做的一些更改是绑定(bind) handleChange函数并更新日期格式(在问题代码中,输入不正确)。
recording-datepicker-gif
我希望这个答案能把你推向正确的方向。

关于reactjs - react 日期选择器和 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com