reactjs - 从父母发送 onMouseDown 给孩子
问题描述
有没有办法将clickevent从父母发送给孩子?
这是我的父组件:
<Component {...props}>
<Child />
{props.children}
</Component>
这是子组件:
<Component onMouseDown={e => this.handleClick(e, props)}></Component>
每当单击父组件时,我都想触发我孩子的 handleclick 组件。
提前致谢!
解决方案
您可以使用对子组件的引用:
// parent.js
constructor(props) {
super(props);
this.child = React.createRef();
}
handleMouseDown = e => {
this.child.current.handleClick(e, this.props);
}
render() {
return (
<Component onMouseDown={this.handleMouseDown} {...props}>
<Child ref={this.child}/>
{props.children}
</Component>
)
}
推荐阅读
- javascript - 在使用邮递员测试基本的 Nodejs CRUD API 时,它给出了错误“无法发布”。如何解决这个问题?
- flutter - ChangeNotifierProvider:可以重新创建其创建的 ChangeNotifier 吗?
- mysql - MySql - 具有特定字段计数的列表
- ruby - 为什么 Ruby setter 返回传递的值,而不是实例变量的最终值?
- git - 在 Github 提交之前:你对文件管理的管家规则是什么?
- python - 如何在张量流中沿选定轴获得等级高于 2 的张量的对角线
- python - Pandas 将列与特定值进行比较
- ios - Swift 继承(扩展类)
- node.js - 邮递员正在上传到 S3,但我的节点应用程序不是
- python - 从另一个导入的文件导入文件