简体   繁体   English

类型 '{ children?: ReactNode; 上不存在属性 'onClick' }'

[英]Property 'onClick' does not exist on type '{ children?: ReactNode; }'

I can't seem to find what I'm doing wrong... Any help will be appreciated:我似乎找不到我做错了什么......任何帮助将不胜感激:

type Props = {
  onClick: () => void, 
  value: string
}

const CustomInput = forwardRef<Props>(({ onClick, value }, ref) => (
  <div className="react-datepicker-custom-input" onClick={onClick}>
    {value}
    <i className={classes.arrowDown}></i>
  </div>
));


error message: Property 'onClick' does not exist on type '{ children?: ReactNode; }

export default CustomInput;导出默认自定义输入;

Props generic should go as a second argument, not the first. Props generic 应该 go 作为第二个参数,而不是第一个。

Example:例子:

type Props = {
  onClick: () => void, 
  value: string
}

type RefType=number
const CustomInput = forwardRef<RefType, Props>(({ onClick, value }, ref) => (
  <div className="react-datepicker-custom-input" onClick={onClick}>
    {value}
    <i className={classes.arrowDown}></i>
  </div>
));

First generic argument of forwardRef is for ref type, second - if for props accordingly forwardRef的第一个通用参数是 ref 类型,第二个 - 如果相应地用于 props

You can type by adding it this way:您可以通过以下方式添加它:

type Props = {
  onClick: () => void, 
  value: string
}

const CustomInput = forwardRef<Props>(({ onClick, value }:Props, ref:any) => (
  <div className="react-datepicker-custom-input" onClick={onClick}>
    {value}
    <i className={classes.arrowDown}></i>
  </div>
));

Try this尝试这个

 interface Props { children: ReactNode; onClick: () => void; value: string; } const CustomInput = forwardRef(({ onClick, value }:Props) => ( <div className="react-datepicker-custom-input" onClick={onClick}> {value} <i className={classes.arrowDown}></i> </div> ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 属性不存在于类型 'IntrinsicAttributes & { children?: ReactNode; }' - Property does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }' "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' 类型 '{ children?: ReactNode; 上不存在属性 'firebase' }' 如何正确注释? - Property 'firebase' does not exist on type '{ children?: ReactNode; }' how to propperly annotate? 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' TypeScript 错误:属性“children”在类型“ReactNode”上不存在 - TypeScript error: Property 'children' does not exist on type 'ReactNode' 类型'IntrinsicAttributes &amp; PhoneInputProps &amp; { children?: ReactNode; 上不存在属性'ref' } - Property 'ref' does not exist on type 'IntrinsicAttributes & PhoneInputProps & { children?: ReactNode; } &#39;IntrinsicAttributes &amp; IntrinsicClassAttributes 类型上不存在属性<DatePicker> &amp; Readonly&lt;{ children?: ReactNode; }&gt; ...&#39; - Property does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<DatePicker> & Readonly<{ children?: ReactNode; }> …' "<i>TypeScript: Property &#39;data&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript:类型&#39;{ children?:ReactNode;上不存在属性&#39;data&#39;<\/b> <i>}&#39;.<\/i> }&#39;。<\/b> <i>ts(2339)<\/i> ts(2339)<\/b>" - TypeScript: Property 'data' does not exist on type '{ children?: ReactNode; }'. ts(2339) 属性 'setUser' 不存在于类型'Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; }&gt;' - Property 'setUser' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>' 类型 &#39;{ props: ReactNode; 上不存在属性 &#39;className&#39; }&#39; - Property 'className' does not exist on type '{ props: ReactNode; }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM