reactjs - 如何知道另一个子组件中一个子组件的事件
问题描述
class Dashboard extends Component {
render() {
return (
<div className="row">
<SidePanel />
<DashboardContentPanel />
</div>
);
}
}
我的问题是:在 sidePanel 中,有 6 个按钮,根据点击的按钮,我想更改 DashboardContentPanel 的内容
我是 ReactJS 的新手!帮我。只是一个逻辑!
解决方案
您可以使用ref
来访问整个组件功能
假设您的DashboardContentPanel
组件有一个调用的函数myFunctionButtonOne
,并且您需要在组件中单击按钮时触发此函数SidePanel
,因此该Dashboard
组件将成为 ( SidePanel
, DashboardContentPanel
) 组件之间的中介
class Dashboard extends Component {
render() {
return (
<div className="row">
<SidePanel handleButtonOne={this.panel.myFunctionButtonOne} />
<DashboardContentPanel ref={(panel)=> this.panel = panel} />
</div>
);
}
}
当我们将 handleButtonOne 作为道具传递时,我们可以像这样使用它
<button onClick={this.props.handleButtonOne}>Click me</button>
推荐阅读
- permissions - 用于访问 Google Cloud Storage 上的 HTML 文件的 Google Data Studio 服务帐户
- python - Windows Docker - 在 python 中安装 Spacy 语言模型返回 ImportError:DLL 加载失败:找不到指定的模块
- azure - 无法将 Service Fabric SDK 4.1.409 安装到 Windows 10 笔记本电脑
- javascript - 使用firebase中的where子句检索数据
- excel - Power Query:如何使 Power Query 允许具有相同名称的列?
- javascript - 单击时如何显示未来图像的实际大小
- oracle - 如何获取 oracle dataguard 日志传输和应用状态?
- dns - 引用相同 A 记录的 CNAME 的不同 nslookup 响应问题
- python - 在 Python 中并行化函数
- image-processing - pytorch affine_grid:theta 输入是什么?