javascript - 在父级别维护 onClick 属性,而不是将其传递给子组件
问题描述
经过半小时的谷歌搜索后,我一无所获。我在我的项目中使用打字稿,并且在这种情况下,我有一个带有孩子的父组件。我想onClick
为该子级设置一个属性,但我希望该属性在父级解析,而不是作为道具传递并在子级解析。
const PasswordField: React.FC<{ name: string; placeholder: string }> = ({ name, placeholder }) => {
const passwordRef = useRef<HTMLInputElement>(null);
const foo = () => {
//do something
}
return (
<FormField name={name} >
<InputField ref={passwordRef} name={name} placeholder={placeholder} type="password" />
<LockIcon width={"20px"} height={"20px"} stroke={theme.colors.supportGrey10} />
// i want foo to run here at parent level, instead of running inside the child component
<EyeIcon width={"20px"} height={"20px"} stroke={theme.colors.supportGrey10} onClick={foo} />
</FormField>
)
};
export default PasswordField
在EyeIcon
组件行上,我收到以下错误:
Property 'onClick' does not exist on type 'IntrinsicAttributes & ISVGProps & { children?: ReactNode; }'.ts(2322)
哪个是对的。我对孩子没有onClick
财产,我也不想。
有没有办法不出现这个错误?对打字稿说该onClick
属性应该在父级解决而不是传递给子级?
解决方案
推荐阅读
- ios - 从 Xcode 10 更新到 Xcode 11.3.1 时出现 Xcode 编译器错误
- python - 在 pyqt5 中退出 GUI 时终止正在运行的进程的正确方法是什么?
- postgresql - 禁用 PostgreSQL 查询优化?
- python - 我如何对从 FTP 服务器下载的 tensorflow 模型使用 .predict 方法?
- java - 登录表单:单击按钮时键盘隐藏,但单击edittext后不再显示
- arrays - 在Google表格中提取单词和字符之间的单词
- sql - 强制 dbGetQuery 返回 POSIXct 时间戳
- .net - Linux Container .Net Core 2.2 上的 JWT 无效签名
- css - 如何在 CSS 中水平对齐多个图像
- java - 需要帮助编写一个函数来确定一个二叉树数组有多少个孩子