React forwardRef - 属性 在 IntrinsicAttributes 类型上不存在
React forwardRef - Property does not exist on type IntrinsicAttributes
我对 ref
有疑问。
我的组件:
type DateInputProps = {
label?: string;
name: string;
defaultValue: Date | null;
maxDate?: Date;
minDate?: Date;
noMargin?: boolean;
forwardRef?: React.RefObject<HTMLInputElement>;
onChange: (date: Date | null) => void;
};
export const DateInput: FC<DateInputProps> = forwardRef<HTMLInputElement, DateInputProps>(
({ defaultValue, label, name, noMargin = false, maxDate, minDate, forwardRef, onChange, ...props }, ref) => {
...
const CustomInput = ({ value, onClick }: { value?: Date; onClick?: () => void }) => (
<Input
ref={ref}
name={name}
defaultValue={value}
onBlur={onBlur}
alignment={InputAlignment.Right}
iconRight={{ onClick, icon: 'calendar' }}
iconLeft={value ? { icon: 'close', onClick: () => setDateValue(null) } : undefined}
/>
);
...
我收到以下错误:
error TS2322: Type '{ ref: (ref: HTMLSelectElement | HTMLInputElement | HTMLTextAreaElement | CustomElement<Record<string, any>> | ... 12 more ... | null) => void; ... 4 more ...; maxDate: Date | undefined; }' is not assignable to type 'IntrinsicAttributes & DateInputProps & { children?: ReactNode; }'.
Property 'ref' does not exist on type 'IntrinsicAttributes & DateInputProps & { children?: ReactNode; }'.
我也尝试使用 forwardRef
作为 属性 - 结果相同。
反应版本:16.14.0
解决方案:react.ForwardedRef<HTMLInputElement>
我对 ref
有疑问。
我的组件:
type DateInputProps = {
label?: string;
name: string;
defaultValue: Date | null;
maxDate?: Date;
minDate?: Date;
noMargin?: boolean;
forwardRef?: React.RefObject<HTMLInputElement>;
onChange: (date: Date | null) => void;
};
export const DateInput: FC<DateInputProps> = forwardRef<HTMLInputElement, DateInputProps>(
({ defaultValue, label, name, noMargin = false, maxDate, minDate, forwardRef, onChange, ...props }, ref) => {
...
const CustomInput = ({ value, onClick }: { value?: Date; onClick?: () => void }) => (
<Input
ref={ref}
name={name}
defaultValue={value}
onBlur={onBlur}
alignment={InputAlignment.Right}
iconRight={{ onClick, icon: 'calendar' }}
iconLeft={value ? { icon: 'close', onClick: () => setDateValue(null) } : undefined}
/>
);
...
我收到以下错误:
error TS2322: Type '{ ref: (ref: HTMLSelectElement | HTMLInputElement | HTMLTextAreaElement | CustomElement<Record<string, any>> | ... 12 more ... | null) => void; ... 4 more ...; maxDate: Date | undefined; }' is not assignable to type 'IntrinsicAttributes & DateInputProps & { children?: ReactNode; }'.
Property 'ref' does not exist on type 'IntrinsicAttributes & DateInputProps & { children?: ReactNode; }'.
我也尝试使用 forwardRef
作为 属性 - 结果相同。
反应版本:16.14.0
解决方案:react.ForwardedRef<HTMLInputElement>