reactjs - 为什么 react-day-picker onDayClick 给出没有重载匹配这个 cal 错误?
问题描述
我对 React 还很陌生,我正在尝试使用 [react-day-picker][1] 作为一个简单的日期选择选项。问题是我复制了 [example] [2] 中的代码,但是 onDayPick 参数有一条波浪线并给了我这个错误
No overload matches this call.
Overload 1 of 2, '(props: DayPickerProps | Readonly<DayPickerProps>): DayPicker', gave the following error.
Type '(day: any, { selected, disabled }: { selected: any; disabled: any; }) => void' is not assignable to type '(day: Date, modifiers: DayModifiers, e: MouseEvent<HTMLDivElement, MouseEvent>) => void'.
Types of parameters '__1' and 'modifiers' are incompatible.
Type 'DayModifiers' is missing the following properties from type '{ selected: any; disabled: any; }': selected, disabled
Overload 2 of 2, '(props: DayPickerProps, context: any): DayPicker', gave the following error.
Type '(day: any, { selected, disabled }: { selected: any; disabled: any; }) => void' is not assignable to type '(day: Date, modifiers: DayModifiers, e: MouseEvent<HTMLDivElement, MouseEvent>) => void'.
这是我的组件,以防我在复制时做错了什么
import React from 'react';
import DayPicker from 'react-day-picker';
import 'react-day-picker/lib/style.css';
export default class DatePicker extends React.Component<{}, {selectedDay: any}>{
constructor(props) {
super(props);
this.state = {
selectedDay: undefined,
}
this.handleDayClick = this.handleDayClick.bind(this);
}
handleDayClick(day, { selected, disabled }) {
if (disabled) {
// Day is disabled, do nothing
return;
}
if (selected) {
// Unselect the day if already selected
this.setState({ selectedDay: undefined });
return;
}
this.setState({ selectedDay: day });
}
render() {
return(
<div>
<DayPicker
onDayClick={this.handleDayClick}
selectedDays={this.state.selectedDay}
/>
{this.state.selectedDay ? (
<p>You clicked {this.state.selectedDay.toLocaleDateString()}</p>
) : (
<p>Please select a day.</p>
)}
</div>
)
}
}
[1]: https://react-day-picker.js.org/
[2]: https://react-day-picker.js.org/docs/basic-concepts
解决方案
它适用于我的 chrome 和一个基本的 react 应用程序。我在 package.json 中使用这些版本:
"react": "^17.0.1",
"react-day-picker": "^7.4.8",
"react-dom": "^17.0.1",
推荐阅读
- azure - 无论出价如何,都会取消分配 Azure 现货实例。相同的非现货实例运行良好
- angular - 在 Angular 8/9/11 中加密和解密本地存储
- excel - 单击单元格后,Excel 更改导出列表的外观
- java - Maven - 带有 TestNG 的黄瓜。通过标签开始黄瓜测试后 - 所有 testng 测试 @Test 与黄瓜一同时开始
- javascript - 通过框架内的链接进行 iframe 导航
- docker - WSL 2 中的 Docker 和与 Visual Studio 的集成
- redux - 如何从prepareHeaders访问给定端点/的rtk查询切片数据?
- python - 在 Python 中使用文本相似性度量基于 AI 的重复数据删除
- python - Python:带有对象列表的 JSON 到数据框
- css - 轮播指示器中的点而不是线 - primeng