javascript - ReactJS 将数据传递给渲染模式?

标签 javascript html reactjs zurb-foundation

我不确定这是否可能,也许我做错了什么。

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

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

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

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

如果我输入 open函数位于 <Modal>我收到一条错误消息: “无法在现有状态转换期间更新(例如在 render 内)。渲染方法应该是 props 和状态的纯函数。”

输出的照片 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;

最佳答案

首先,在将组件上下文用作事件处理程序时,您应该将组件上下文绑定(bind)到 open 方法(使用 null 的 this insetad 作为第一个绑定(bind)参数):

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>

关于javascript - ReactJS 将数据传递给渲染模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724464/

相关文章:

javascript - React 处理多输入值

Javascript 性能以及比较对象与对象的属性

javascript - 使用 ReactJS 的操作处理程序中的 setState 未更新

html - 如何使所有导航链接看起来都一样?

javascript - 初始化两张传单 map - 一张重写另一张

javascript - native 无法找到图像 './expo-asset/assets/${your_img_path}' 在 React Native/Expo 中找不到

javascript - 防止重新渲染 react 对象数组

javascript - 使用 formatter.js 时,React onChange 不会在文本输入时触发

javascript - TypeError : this. props.AddNewHero 不是一个函数

html - 如何使用 CSS 将背景图像调整为整个窗口宽度