interface中包含可选属性,且是Function类型的;在dom中怎么绑定函数?
来源:4-8 【讨论题】编写基础请求代码

qq_一袖清风_0
2021-04-08
老师你好,ts刚开始用,还不太熟悉,如下场景,该怎么实现上述的功能呢?
interface IProps {
canGoBack?: boolean
title?: string
tapAction?: Function // 传入的方法
hasBackgroundImage?: boolean,
children: any
}
const App = (props: IProps) => {
const { tapAction } = props;
// 默认方法
const goBack = () => {};
// 当前写法是报错的,这里该怎么写?
return <div onClick={tapAction ?? goBack}>click</div>;
}
写回答
2回答
-
ustbhuangyi
2021-04-09
所以你的 tapAction 应该定义成具体的 (event: ITouchEvent<any>): void 类型才对吧
00 -
ustbhuangyi
2021-04-09
具体报错的内容是什么?
012021-04-09
相似问题