[英]React.js Onclick event on a dynamically generated button is not firing
So I am new to React.js and have gotten stuck on writing a fucntionality. 因此,我是React.js的新手,并且被困在编写功能上。 I have a form in which i am trying to add a set of data a numerical value & a web address.
我有一种表单,试图在其中添加一组数据,数值和网址。 Once a user clicks the add button, a row is dynamically generated within the form with the values displayed and option to either edit or delete the said value.
用户单击添加按钮后,将在表单中动态生成一行,其中显示的值以及用于编辑或删除所述值的选项。 The user can add as many of these rows as they want.
用户可以根据需要添加任意数量的这些行。
The code to generate these dynamic rows is below 生成这些动态行的代码如下
let orderStandardDetails = '<div id={standardInfo_'+orderStandardInformationChildNodesLength+'} class="standardInfo"><br /><input id="orderStandard_'+orderStandardInformationChildNodesLength+'" class="orderStandardInfo" type="text" value="ISO '+orderStandard.toString()+'" name="standardInfo" disabled/>' + '<input id={orderStandardUrl_'+orderStandardInformationChildNodesLength+'} type="text" class="orderStandardUrlInfo" value="'+orderStandardUrl+'" disabled/>' + '<button class="orderStandardEdit" id={orderStandardEdit_'+orderStandardInformationChildNodesLength+'} onClick={this.editOrderStandard}><img src="app/images/Edit.svg" /></button>' + '<button class="orderStandardDelete" id={orderStandardDelete_'+orderStandardInformationChildNodesLength+'} onClick={this.deleteOrderStandard()}><img src="app/images/Delete.svg" /></button>' + '<button class="orderStandardSave" id={orderStandardSave_'+orderStandardInformationChildNodesLength+'} onClick={this.saveOrderStandard()}><img src="app/images/Save.svg" /></button></div>';
Also, I am binding the function as follows inside the constructor 另外,我在构造函数中将函数绑定如下
this.editOrderStandard = this.editOrderStandard.bind(this); this.deleteOrderStandard = this.deleteOrderStandard.bind(this); this.saveOrderStandard = this.saveOrderStandard.bind(this);
The said function is defined as follows 所述功能定义如下
editOrderStandard(){ alert("Edit"); } deleteOrderStandard(){ alert("Delete"); } saveOrderStandard(){ alert("Save"); }
The issue is that none of the above three functions are firing on clicking teh button. 问题是,单击这三个按钮时,以上三个功能均未激活。 Moreover, in the firefox console i cannot see any errors pertaining to onclick.
此外,在firefox控制台中,我看不到任何与onclick有关的错误。
Please help. 请帮忙。
First things first, you should avoid to do DOM manipulations and let react take care of it.
首先,您应该避免进行DOM操作,而让react处理它。 As suggested to you in comments, you should use JSX.
如注释中建议的那样,您应该使用JSX。 For more info: JSX
有关更多信息: JSX
You can create stateless components which you want to dynamically render and let the parent component take care of state changes.
您可以创建要动态呈现的无状态组件,并让父组件负责状态更改。 Below sample is for reference.
下面的示例仅供参考。
const OrderStandardDetails =(props) => { return ( <div class = "standardInfo"> <input class = "orderStandardInfo" type = "text" value = "ISO" name = "standardInfo" disabled / > <input type = "text" class = "orderStandardUrlInfo" value = "" disabled / > <button class = "orderStandardEdit" onClick = {props.onEdit}>Edit</button> </div> ); }; class App extends React.Component { constructor(props) { super(props); this.onEdit = this.onEdit.bind(this) } onEdit() { alert("Edit"); } render() { return <div> <OrderStandardDetails onEdit={this.onEdit} /> </div> }; } ReactDOM.render( < App / > , document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div>
If you're using redux, check out redux-form 's fieldArray . 如果您使用的是redux,请查看redux-form的fieldArray 。 I think the example provides functionality similar to what you are looking for.
我认为该示例提供的功能与您正在寻找的功能相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.