最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Dynamic import react-datepicker with nextdynamic - Stack Overflow

programmeradmin0浏览0评论

I want to dynamic import react-datepicker with the following:

import dynamic from 'next/dynamic';
const DatePicker = dynamic(() => import('react-datepicker').then(mod => mod.default), { ssr: false });

But it always gives error:

Argument of type '() => Promise<typeof DatePicker>' is not assignable to parameter of type 'DynamicOptions<DatePickerProps> | Loader<DatePickerProps>'.
  Type '() => Promise<typeof DatePicker>' is not assignable to type '() => LoaderComponent<DatePickerProps>'.
    Type 'Promise<typeof DatePicker>' is not assignable to type 'LoaderComponent<DatePickerProps>'.
      Type 'typeof DatePicker' is not assignable to type 'ComponentType<DatePickerProps> | ComponentModule<DatePickerProps>'.
        Type 'typeof DatePicker' is not assignable to type 'ComponentClass<DatePickerProps, any>'.
          Types of property 'defaultProps' are incompatible.
            Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<DatePickerProps> | undefined'.
              Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<Omit<{ children?: ReactNode; } & Omit<YearDropdownProps, "date" | "onChange" | "year" | "minDate" | "maxDate"> & Omit<MonthDropdownProps, "onChange" | "month"> & ... 9 more ... & Pick<...>, "dropdownMode" | ... 19 more ... | "onTimeChange"> & ... 4 more ... & { ...; }>'.
                Types of property 'customTimeInput' are incompatible.
                  Type 'null' is not assignable to type 'Element | undefined'.ts(2345)

I removed the method chaining as suggested and the error becomes:

Argument of type '() => Promise<typeof import("<Project>/node_modules/react-datepicker/dist/index")>' is not assignable to parameter of type 'DynamicOptions<DatePickerProps> | Loader<DatePickerProps>'.
  Type '() => Promise<typeof import("<Project>/node_modules/react-datepicker/dist/index")>' is not assignable to type '() => LoaderComponent<DatePickerProps>'.
    Type 'Promise<typeof import("<Project>/node_modules/react-datepicker/dist/index")>' is not assignable to type 'LoaderComponent<DatePickerProps>'.
      Type 'typeof import("<Project>/node_modules/react-datepicker/dist/index")' is not assignable to type 'ComponentType<DatePickerProps> | ComponentModule<DatePickerProps>'.
        Type 'typeof import("<Project>/node_modules/react-datepicker/dist/index")' is not assignable to type 'ComponentModule<DatePickerProps>'.
          Types of property 'default' are incompatible.
            Type 'typeof DatePicker' is not assignable to type 'ComponentType<DatePickerProps>'.
              Type 'typeof DatePicker' is not assignable to type 'ComponentClass<DatePickerProps, any>'.
                Types of property 'defaultProps' are incompatible.
                  Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<DatePickerProps> | undefined'.
                    Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<Omit<{ children?: ReactNode; } & Omit<YearDropdownProps, "onChange" | "date" | "year" | "minDate" | "maxDate"> & Omit<MonthDropdownProps, "onChange" | "month"> & ... 9 more ... & Pick<...>, "className" | ... 19 more ... | "onTimeChange"> & ... 4 more ... & { ...; }>'.
                      Types of property 'customTimeInput' are incompatible.
                        Type 'null' is not assignable to type 'Element | undefined'.ts(2345)

So how can I use dynamic import properly ?

Any help would be greatly appreciated.

Best regards,

SetNug

I want to dynamic import react-datepicker with the following:

import dynamic from 'next/dynamic';
const DatePicker = dynamic(() => import('react-datepicker').then(mod => mod.default), { ssr: false });

But it always gives error:

Argument of type '() => Promise<typeof DatePicker>' is not assignable to parameter of type 'DynamicOptions<DatePickerProps> | Loader<DatePickerProps>'.
  Type '() => Promise<typeof DatePicker>' is not assignable to type '() => LoaderComponent<DatePickerProps>'.
    Type 'Promise<typeof DatePicker>' is not assignable to type 'LoaderComponent<DatePickerProps>'.
      Type 'typeof DatePicker' is not assignable to type 'ComponentType<DatePickerProps> | ComponentModule<DatePickerProps>'.
        Type 'typeof DatePicker' is not assignable to type 'ComponentClass<DatePickerProps, any>'.
          Types of property 'defaultProps' are incompatible.
            Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<DatePickerProps> | undefined'.
              Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<Omit<{ children?: ReactNode; } & Omit<YearDropdownProps, "date" | "onChange" | "year" | "minDate" | "maxDate"> & Omit<MonthDropdownProps, "onChange" | "month"> & ... 9 more ... & Pick<...>, "dropdownMode" | ... 19 more ... | "onTimeChange"> & ... 4 more ... & { ...; }>'.
                Types of property 'customTimeInput' are incompatible.
                  Type 'null' is not assignable to type 'Element | undefined'.ts(2345)

I removed the method chaining as suggested and the error becomes:

Argument of type '() => Promise<typeof import("<Project>/node_modules/react-datepicker/dist/index")>' is not assignable to parameter of type 'DynamicOptions<DatePickerProps> | Loader<DatePickerProps>'.
  Type '() => Promise<typeof import("<Project>/node_modules/react-datepicker/dist/index")>' is not assignable to type '() => LoaderComponent<DatePickerProps>'.
    Type 'Promise<typeof import("<Project>/node_modules/react-datepicker/dist/index")>' is not assignable to type 'LoaderComponent<DatePickerProps>'.
      Type 'typeof import("<Project>/node_modules/react-datepicker/dist/index")' is not assignable to type 'ComponentType<DatePickerProps> | ComponentModule<DatePickerProps>'.
        Type 'typeof import("<Project>/node_modules/react-datepicker/dist/index")' is not assignable to type 'ComponentModule<DatePickerProps>'.
          Types of property 'default' are incompatible.
            Type 'typeof DatePicker' is not assignable to type 'ComponentType<DatePickerProps>'.
              Type 'typeof DatePicker' is not assignable to type 'ComponentClass<DatePickerProps, any>'.
                Types of property 'defaultProps' are incompatible.
                  Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<DatePickerProps> | undefined'.
                    Type '{ allowSameDay: boolean; dateFormat: string; dateFormatCalendar: string; disabled: boolean; disabledKeyboardNavigation: boolean; dropdownMode: "scroll"; preventOpenOnFocus: boolean; monthsShown: number; ... 35 more ...; usePointerEvent: boolean; }' is not assignable to type 'Partial<Omit<{ children?: ReactNode; } & Omit<YearDropdownProps, "onChange" | "date" | "year" | "minDate" | "maxDate"> & Omit<MonthDropdownProps, "onChange" | "month"> & ... 9 more ... & Pick<...>, "className" | ... 19 more ... | "onTimeChange"> & ... 4 more ... & { ...; }>'.
                      Types of property 'customTimeInput' are incompatible.
                        Type 'null' is not assignable to type 'Element | undefined'.ts(2345)

So how can I use dynamic import properly ?

Any help would be greatly appreciated.

Best regards,

SetNug

Share Improve this question edited Feb 7 at 12:00 SetNug asked Feb 6 at 17:37 SetNugSetNug 4031 gold badge4 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try this

import dynamic from 'next/dynamic';
const DatePicker = dynamic(() => import('react-datepicker'), { ssr: false })
// You don't need to chain import result with "then" because "then"
// returns a Promise, which causes a type compatibility conflict with
// dynamic function's first argument return type
发布评论

评论列表(0)

  1. 暂无评论