例如,我有一个具有两种绑定方法的react组件:
import React from 'react';
class Comments extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleRemoveComment = this.handleRemoveComment.bind(this);
}
handleSubmit(e) {
.....
}
handleRemoveComment(e) {
//this.props.removeComment(null, this.props.params, i);
}
renderComment(comment, i) {
return(
<div className="comment" key={i}>
.....
<button
onClick={this.handleRemoveComment}
className="remove-comment">
×
</button>
</div>
)
}
render() {
return(
<div className="comments">
{this.props.postComments.map(this.renderComment)}
.....
</div>
)
}
}
export default Comments;
在上面的代码中,我有两种绑定方法:一种是handleSubmit
,一种是handleRemoveComment
。handleSubmit
功能有效但handleRemoveComment
没有。运行时,返回错误:
类型错误:无法读取未定义的属性“handleRemoveComment”