reactjs - 将点击元素的信息作为道具传递给模式 - Reactjs
问题描述
我想要访问模式上单击项目的 id 等属性。如何在单击时将目标属性作为道具传递给模态。
下面是我正在尝试的。不幸的是,模态将 targetElement 属性显示为未定义的componentDidMount
函数CropperModal
export default class DynamicArticleList extends React.Component {
constructor(){
super();
this.state={
targetElement: '',
}
this.showModal = this.showModal.bind(this);
this.hideModal = this.hideModal.bind(this);
}
showModal(e){
this.setState({
targetElement: e.target.getAttribute("id")
})
}
hideModal(e){
this.setState({
showing: false,
showSource: '#',
})
}
render() {
return (
<div className="wrapper container-fluid DynamicArticleList">
<div className="width-control">
<img src="../img/journey.jpg" onDoubleClick={this.showModal} id="Img0"/>
<CropperModal targetElement={this.state.targetElement}/>
</div>
</div>
);
}
}
解决方案
这就是我的做法。我基本上避免模态组件渲染,直到点击事件被处理
export default class DynamicArticleList extends React.Component {
constructor(){
super();
this.state={
showing: false,
targetElement: '',
}
this.showModal = this.showModal.bind(this);
this.hideModal = this.hideModal.bind(this);
}
showModal(e){
this.setState({
showing: true,
targetElement: e.target.getAttribute("id")
})
}
hideModal(e){
this.setState({
showing: false,
showSource: '#',
})
}
render() {
return (
<div className="wrapper container-fluid DynamicArticleList">
<div className="width-control">
<img src="../img/journey.jpg" onDoubleClick={this.showModal} id="Img0"/>
{this.state.showing ? <CropperModal targetElement={this.state.targetElement}/> : null}
</div>
</div>
);
}
}
推荐阅读
- wpf - WPF 的父容器中存在多个文本块时的字符省略号
- node.js - 如何更新mongodb中的数据
- visual-studio-code - Launch.json:如何引用环境变量
- gridview - DevExpress Gridview 选择计数为 1 但未选择行
- python - python正则表达式获取“target = x”的第一个实例
- java - 如何将站点的主页重定向到 /public/index.html 路径
- javascript - 有没有办法在使用 map() 函数的循环中推送谷歌地球引擎中的键值对?
- ios - 低功耗模式下的 UNNotificationAction 处理程序
- javascript - 您可以使用表达式 - arrayName[“cityName”] 找到所有城市,因为它是一个对象
- wmi - Inno Setup:继承的 OLE 对象属性不可访问?