[英]Property '...' does not exist on type 'IntrinsicAttributes & Props'
I have a <InputField>
component in my app with the following type definition for the props:我的应用程序中有一个
<InputField>
组件,其道具类型定义如下:
interface InputFieldProps extends React.HTMLAttributes<HTMLInputElement> {
customProp: string;
}
My component looks like this:我的组件如下所示:
const InputField: React.FC<InputFieldProps> = ({ customProp, ...htmlProps }) => {
return (
<input {...htmlProps} />
);
};
I would expect that I can now pass the prop disabled
or required
to that component, as these properties are part of the HTMLInputElement type definition.我希望我现在可以将 prop
disabled
或required
传递给该组件,因为这些属性是 HTMLInputElement 类型定义的一部分。 However, I get the error:但是,我收到错误:
Property 'disabled' does not exist on type 'IntrinsicAttributes & Props'
“IntrinsicAttributes & Props”类型上不存在属性“已禁用”
I tried passing disabled as disabled={true}
as well as just disabled
with no success.我尝试将 disabled 传递为
disabled={true}
以及只是disabled
但没有成功。 I can, however, pass placeholder
as a prop.但是,我可以将
placeholder
作为道具传递。 So some properties in the HTMLInputElement type definition seem to work, while others don't.因此,HTMLInputElement 类型定义中的某些属性似乎有效,而其他的则无效。
Using React.InputHTMLAttributes<HTMLInputElement>
and making sure every additional property, such as customProp
does not reach your input
.使用
React.InputHTMLAttributes<HTMLInputElement>
并确保每个附加属性(例如customProp
都不会到达您的input
。 In the below example, because customProp
is destructed on its own, htmlProps
would include only input
properties.在下面的示例中,因为
customProp
是自行销毁的, htmlProps
将只包含input
属性。
interface InputFieldProps extends React.InputHTMLAttributes<HTMLInputElement> {
customProp: string;
}
const InputField: React.FC<InputFieldProps> = ({
customProp,
...htmlProps
}) => {
return <input {...htmlProps} />;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.