ReactJS 将数据传递给呈现的模态?
ReactJS Passing data to a rendered modal?
我不确定这是否可行,也许我做错了什么。
我有一个循环,它将一些进度条元素推入数组,然后用信息呈现它。
点击不同的进度条将输出传递给该特定进度条的数据,但它会显示在控制台上。
我不想在控制台上输出它,而是希望该信息在 <Modal>
中动态弹出。
因此,如果用户单击进度条 1,则在进度条 1 中传递的信息将显示在 <Modal>
。
如果我将 open
函数放在 <Modal>
中,我会收到一条错误消息:
"Cannot update during an existing state transition (such as within render
). Render methods should be a pure function of props and state."
一张输出照片
这是我的代码:
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;
首先,当将组件上下文用作事件处理程序时,您应该将组件上下文绑定到 open
方法(使用 this
null 插入作为第一个绑定参数):
onClick={this.open.bind(this, "System2", systemValues[0], name[0], individualSysValueOutput[0])}
那么你应该将点击进度对应的数据存储在state:
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>
我不确定这是否可行,也许我做错了什么。
我有一个循环,它将一些进度条元素推入数组,然后用信息呈现它。
点击不同的进度条将输出传递给该特定进度条的数据,但它会显示在控制台上。
我不想在控制台上输出它,而是希望该信息在 <Modal>
中动态弹出。
因此,如果用户单击进度条 1,则在进度条 1 中传递的信息将显示在 <Modal>
。
如果我将 open
函数放在 <Modal>
中,我会收到一条错误消息:
"Cannot update during an existing state transition (such as within render
). Render methods should be a pure function of props and state."
一张输出照片
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;
首先,当将组件上下文用作事件处理程序时,您应该将组件上下文绑定到 open
方法(使用 this
null 插入作为第一个绑定参数):
onClick={this.open.bind(this, "System2", systemValues[0], name[0], individualSysValueOutput[0])}
那么你应该将点击进度对应的数据存储在state:
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>