ReactJS将数据传递给渲染模态?

时间:2017-03-10 17:35:25

标签: javascript html reactjs zurb-foundation

我不确定这是否可行,也许我做错了。

我所拥有的是一个循环,它将一些进度条元素推入数组,然后使用该信息进行渲染。

单击不同的进度条将输出传递给该特定进度条的数据,但这会显示在控制台上。

我希望动态地在<Modal>中弹出这些信息,而不是在控制台上输出它。

因此,如果用户点击进度条编号1,则进度条编号1中传递的信息将显示在<Modal>中。

如果我将open函数放入<Modal>,我会收到错误消息: “无法在现有状态转换期间更新(例如在render内)。渲染方法应该是道具和状态的纯函数。”

输出的照片 enter image description here 这是我的代码:

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;

1 个答案:

答案 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>