首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么react-day-picker onDayClick给出No overload matches这个调用错误?

为什么react-day-picker onDayClick给出No overload matches这个调用错误?
EN

Stack Overflow用户
提问于 2021-01-20 21:55:21
回答 1查看 150关注 0票数 0

我是React的新手,我正在尝试使用react-day-picker来实现一个简单的日期选择选项。问题是我复制了示例中的代码,但是onDayPick参数有一条弯弯曲曲的线,并给出了这个错误

代码语言:javascript
复制
    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'.

这是我的组件,以防我在复制时出错

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-01-20 22:15:22

它适用于我的chrome和一个基本的react应用程序。我在package.json中使用以下版本:

代码语言:javascript
复制
"react": "^17.0.1",
"react-day-picker": "^7.4.8",
"react-dom": "^17.0.1",
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65810940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档