gpt4 book ai didi

javascript - 尝试将 onChange 函数作为 props 传递给 GrandChlidren 结果导致 TypeError : this is undefined

转载 作者:行者123 更新时间:2023-11-28 18:35:04 25 4
gpt4 key购买 nike

我试图将父级的 onChange 函数传递给 grandChild 但出现错误

类型错误:这是未定义的

代码如下

var Tablefortask = React.createClass({

getInitialState: function() {
return {data: []};

},
onChange: function (e) {

var employeeId = e.target.value;
alert(employeeId);

},

render: function() {

return (
<div className="border-basic-task col-md-12">
<div className="col-md-12">
<table className="table table-condensed table-bordered table-striped-col " id="table-data">
<thead>
<tr align="center">
<th>Task Name</th>
<th >Standard Discription of Task</th>
<th>Employee Comment</th>
<th >Employee Rating</th>
<th width="30%">Reviewer Comment</th>
<th >Reviewer Rating</th>
</tr>
</thead>
<TablefortaskList data={this.state.data} onChange={this.onChange} />
</table>
</div>
</div>
);
}
});

var TablefortaskList = React.createClass({
render: function() {
var commentNodes = this.props.data.map(function(comment) {
return (
<Addcontenttotable onChange= {this.props.onChange} taskName={comment.taskName} standarDescription={comment.standarDescription} emplComment={comment.emp_comment} empRating={comment.empRating} key={comment.id} reviewComment={comment.review_comment} reviewRating={comment.review_rating} >
</Addcontenttotable>
);
});
return (
<tbody>{commentNodes}</tbody>
);
}
});


var Addcontenttotable = React.createClass({
render: function() {
if(this.props.reviewComment=== "" && this.props.reviewRating === '' ){
return (
<tr><td>{this.props.taskName}</td>
<td>{this.props.standarDescription}</td>
<td>{this.props.emplComment}</td>
<td width="5%">{this.props.empRating}</td>
<td ><textarea
className="form-control"
name="empComment"
placeholder="Employee Comment"
/>
</td>
<td>
<select
className="form-control"
onChange={this.props.onChange}
data-placeholder="Basic Select2 Box" >
<option value="">Option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
</tr>

);
}
else {
return (
<tr><td>{this.props.taskName}</td>
<td>{this.props.standarDescription}</td>
<td>{this.props.emplComment}</td>
<td>{this.props.empRating}</td>
<td>{this.props.reviewComment}</td>
<td>{this.props.reviewRating}</td>
</tr>
);
}
}
});

任何见解将不胜感激

最佳答案

发生这种情况是因为this不是引用 map 函数内的组件。

要修复此问题,您必须绑定(bind)上下文,将其作为第二个参数传递给 map 函数。

 var commentNodes = this.props.data.map(function(comment) {
return (
<Addcontenttotable onChange= {this.props.onChange} taskName={comment.taskName} standarDescription={comment.standarDescription} emplComment={comment.emp_comment} empRating={comment.empRating} key={comment.id} reviewComment={comment.review_comment} reviewRating={comment.review_rating} >
</Addcontenttotable>
);
},this);

如果您使用 ES6,您可以使用粗箭头函数来保留上下文。

 var commentNodes = this.props.data.map((comment) =>  {
return (
<Addcontenttotable onChange= {this.props.onChange} taskName={comment.taskName} standarDescription={comment.standarDescription} emplComment={comment.emp_comment} empRating={comment.empRating} key={comment.id} reviewComment={comment.review_comment} reviewRating={comment.review_rating} >
</Addcontenttotable>
);
});

jsfiddle with the problem solved

关于javascript - 尝试将 onChange 函数作为 props 传递给 GrandChlidren 结果导致 TypeError : this is undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37255421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com