我不确定这是否可行,也许我做错了。
我所拥有的是一个循环,它将一些进度条元素推入数组,然后使用该信息进行渲染。
单击不同的进度条将输出传递给该特定进度条的数据,但这会显示在控制台上。
我希望动态地在<Modal>
中弹出这些信息,而不是在控制台上输出它。
因此,如果用户点击进度条编号1,则进度条编号1中传递的信息将显示在<Modal>
中。
如果我将open
函数放入<Modal>
,我会收到错误消息:
“无法在现有状态转换期间更新(例如在render
内)。渲染方法应该是道具和状态的纯函数。”
var React = require('react');
var Modal = require('react-overlays/lib/Modal');
var TableResults = React.createClass({
close(){
this.setState({ showModal: false });
},
open: function(system, value, name, individualValues){
this.setState({ showModal: true });
console.log(system);
console.log("Story: " + name);
console.log("Value:" + individualValues);
console.log("Total Output: " + value);
console.log("=============================================");
},
render: function(){
loop with logic and calculations{
bodyRows[cellIndex].push(
<td id="tableBody">
<div className="progress" role="progressbar" id="progressBarStyle"
onClick={this.open.bind(null, "System2", systemValues[0], name[0], individualSysValueOutput[0])}>
<div className="progress-meter" data-values={this.calculatePercent(systemValues[0])}>
{(Math.round(this.calculatePercent(systemValues[0]) * 100) / 100) + '%'}
</div>
</div>
</td>
)
}
return(
<div>
<Modal
aria-labelledby='modal-label'
style={modalStyle}
backdropStyle={backdropStyle}
show={this.state.showModal}
onHide={this.close}
keyboard={true}
>
<div style={dialogStyle()}>
<table>
<thead>
<tr>
<th>Head</th>
</tr>
</thead>
<tbody>
<tr>
<td>BodyRow</td>
</tr>
<tr>
{/* <td>{this.open()}</td> */}
</tr>
</tbody>
</table>
</div>
</Modal>
<div className="dataTable" >
<table className="hover">
<tbody>
<tr>
{/* Progress bar will be rendered here */}
{bodyRows[0]}
</tr>
<tr>
</tr>
</tbody>
</table>
</div>
</div>
)
});
module.exports = TableResults;
答案 0 :(得分:3)
首先,在将组件上下文用作事件处理程序时,应将组件上下文绑定到open
方法(使用this
insetad of null作为第一个绑定参数):
onClick={this.open.bind(this, "System2", systemValues[0], name[0], individualSysValueOutput[0])}
然后,您应该将与点击进度相对应的数据存储在状态:
open: function(system, value, name, individualValues){
this.setState({ showModal: true,
system,
value,
name,
individualValues
});
}
现在您可以使用模态中的状态数据,如下所示:
<Modal
aria-labelledby='modal-label'
style={modalStyle}
backdropStyle={backdropStyle}
show={this.state.showModal}
onHide={this.close}
keyboard={true}
>
<div style={dialogStyle()}>
<table>
<thead>
<tr>
<th>Head</th>
</tr>
</thead>
<tbody>
<tr>
<td>BodyRow</td>
</tr>
<tr>
<td>{this.state.value}</td>
</tr>
</tbody>
</table>
</div>
</Modal>