【发布时间】:2019-03-06 04:54:41
【问题描述】:
我是react js 的新手。在这里,我确实有一个模态,就像 ->
ErrorComponent.js
import React from 'react';
export default class ErrorComponent extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
)
}
}
**LowLeveLCriteria.js**
class LowLevelCriteria extends React.Component {
constructor(props) {
super(props);
this.state = {
technologies: this.props.techData,
lowData: this.props.lowData.low,
showModal: false
}
}
validate (v1, v2) { return "1" }
onaddRow(e, id) {
const errors = this.validate("value1", "value2");
if (errors === "1") {
} else {
this.setState({
showModal: true
})
}
render() {
return (
<div>
<div className="questionLevelIndication">
<span className="levelIndicatorBtn backgroundColorForLow">
1
</span>
<label className="levelIndicationLabel">
Low Difficulty Level - Maximum 6 questions
</label>
</div>
{(this.props.lowData) && this.props.lowData.Low.length > 0 && this.props.lowData.Low.map(data => (
<LowRow technologies={this.state.technologies} onChange={this.onchange.bind(this)} data={data} key={data.id} onAddRow={this.onaddRow.bind(this)} onRemoveRow={this.onRemoveRow.bind(this)} />
))}
{this.state.showModal && <ErrorComponent />}
</div>
)
}
<button type="button" className="btn btn-primary btn-sm standard-btn-50 margin-left-10" onClick={(e) => { props.onAddRow(e, props.data.id) }}>+</button>
现在,在这里,当我单击作为子元素的按钮,然后在父组件中调用 AddRow 的方法时。现在,在这里,我想做的是,
如果单击该按钮后,我在 add 函数中调用了另一个函数,该函数验证然后返回一些东西。如果有一个值为“1”,那么我想向用户显示该模式。所以,
我没有办法。我怎样才能做到这一点 ?因为要打开一个模式,我们需要一个数据目标,它需要在按钮上,但我的按钮在另一个组件中。所以,
我尝试使用 document.getElementById,然后在单击按钮后添加数据目标属性。但没有运气。谁能给我一点提示?
【问题讨论】:
-
抱歉,您的问题似乎不清楚。您是否尝试使用 react 显示对话框但失败了?还是有具体问题(添加数据目标?)?
-
我的意思是我最初没有在按钮上添加 data-target 属性。但是当用户单击该按钮并且如果某些条件失败时,我想向用户显示一个对话框。但是对于对话框,我需要在按钮本身上有 data target 属性。在某些条件失败后如何添加该属性,然后显示该模型。
-
我想你正在寻找 boostrap 模态框,这里是使用 react boostrap 的简单模态框链接。 codepen.io/nsieber/pen/grGpzW
-
与您的代码类似的另一种方式是:jsfiddle.net/sqfhkdcy。希望这两个解决方案对您有所帮助。
标签: javascript html reactjs react-redux