试图将onChange函数作为道具传递给GrandChlidren结果进入TypeError:这是未定义的

时间:2016-05-16 13:43:47

标签: javascript reactjs

我正在尝试将父级的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> ); } } });

代码如下

{{1}}

任何见解都将受到赞赏

3 个答案:

答案 0 :(得分:1)

每当你得到一个&#39; 这个&#39;错误,你有第一个解决方案!

为您的班级定义构造函数,如下所示:

var variable = React.createClass({
    ctor: function(){
        this = self;
    },
    func1: function(){
        //your code
        this.anything//error
        self.anything//solved
    },
    func2: function(){
        //your code
        this.anything//error
        self.anything//solved
    },
    func3: function(){
        //your code
        this.anything//error
        self.anything//solved
    }
});

您只需更改所有&#39; &#39;进入&#39; 自我&#39;一旦你启动了如上图所示的ctor功能!

答案 1 :(得分:1)

这种情况正在发生,因为 引用地图功能中的组件。

要修复它,你必须将传递它的上下文作为第二个参数绑定到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

答案 2 :(得分:1)

this传递到地图功能this.props.data.map(function(comment) { }, this);或定义var self = this并在地图功能中使用self.props

 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>
                  );
              }, this);
              return (
                   <tbody>{commentNodes}</tbody>
              );
        }
});