[英]ReactJS | Typescript type for onClick. `onclick does not exist on type`x`
I have the below method, that renders a delete Icon, and I use it in my main Container. 我有以下方法,呈现一个删除图标,并在主容器中使用它。 Everything works fine.
一切正常。 The only problem, I have, and it is a small cosmetic one, is an
any
type that I cannot figure out, what that type is. 我所遇到的唯一问题是一个小小的装饰性问题,是我无法弄清楚的
any
类型,那是什么类型。
import React from 'react';
import Icon from './Icon';
const DeleteActionRenderer = (options: any): Function => (cell: string, row: string): JSX.Element => {
const deleteActionClick = options.onClick({ cell, row }); // The error is here. On options.onClick.
return (
<div>
<a href="#" className="text-danger p-1 text-lg" onClick={deleteActionClick}>
<Icon icon="trash" />
</a>
</div>
);
};
export default DeleteActionRenderer;
As you can see, options
have any
as a type. 如您所见,
options
具有any
类型。 I cannot figure out, what options is. 我不知道有什么选择。 Nothing except any works.
除了任何作品。
object, string, number, Function, MouseEvents, SyntheticEvent
. object, string, number, Function, MouseEvents, SyntheticEvent
。 Not even unknown
. 甚至
unknown
。
The only type that works is any
. 唯一有效的类型是
any
。
I console.log(options, typeOf options), and it prints out an object. 我console.log(options,typeOf options),它打印出一个对象。 See below:
见下文:
{onClick: ƒ}
onClick: ƒ (_ref2)
arguments: (...)
caller: (...)
length: 1
name: ""
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: index.tsx:146
[[Scopes]]: Scopes[5]
__proto__: Object
Any ideas? 有任何想法吗? This
any
is like a sore thumb, for a little while now. 这
any
是像突兀,一小会儿吧。 I cannot figure it out at all. 我根本无法弄清楚。 Thank you for your time.
感谢您的时间。
The code above looks to me like a HOC (High Order Component) https://reactjs.org/docs/higher-order-components.html 上面的代码在我看来像HOC(高阶组件) https://reactjs.org/docs/higher-order-components.html
I can see in your code that the way you are using DeleteActionRenderer
is not correct as the options
object in this case is part of props so it will be coming in the function that you return from DeleteActionRenderer
and not in DeleteActionRenderer
itself. 我在您的代码中看到您使用
DeleteActionRenderer
的方式是不正确的,因为在这种情况下, options
对象是道具的一部分,因此它将出现在您从DeleteActionRenderer
返回的函数中,而不是在DeleteActionRenderer
本身中。
In your case, for options
type will be: 在您的情况下,对于
options
类型将为:
type Options = {
onClick: Function
}
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.